通过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的第二个兄弟节点

posted on 2020-05-13 19:20  愚哥  阅读(1299)  评论(0)    收藏  举报

导航