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的第二個兄弟節點