通过xpath查找android app控件的几个写法


1、使用包含关系定位元素:

self.dr.find_element_by_xpath("//android.widget.TextView[contains(@text,'地铁')]")
self.d.get_ui_by_xpath("//*[@class='android.widget.Button' and contains(@text,'允许')]").click()

2、使用属性值定位元素:

self.dr.find_element_by_xpath("//android.widget.TextView[@text='地铁']")

3、打兄弟元素,特别小心uiautomator上面显示的是0、1、2、3算,但实际xpath是从1开始算的

self.driver.find_element_by_xpath('//*[@text="闪兑"]/../../../android.view.View[4]/android.view.View[4]').text

4、使用下标,说明是第几个元素:

self.dr.find_element_by_xpath("//android.widget.LinearLayout/android.widget.RelativeLayout[1]")

5、使用语法查找兄弟节点(following-sibling是选取当前节点之后的所有节点;preceding-sibling是选择当前节点之前的所有节点):

self.dr.find_element_by_xpath('((//*[@text="com.test.demo"])/following-sibling::TextView)[2]')           # 查找到text为com.test.demo元素后面类型为textview的第二个兄弟节点


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM