xpath语法
https://www.runoob.com/xpath/xpath-syntax.html
xpath层级定位
- 父节点定位子节点
- 子节点定位父节点(
/..
) - 子节点定位兄弟节点
- 爷爷节点定位孙子节点
案例
-
定位股票
xpath //*[@resource-id='com.xueqiu.android:id/title_container']/android.widget.TextView[2]/..
-
定位0988股票的价格
//*[@text='09988']/../../..//*[@resource-id='com.xueqiu.android:id/current_price']
uiautomator定位表达式(一般不用)
- 参考资料
https://developer.android.com/reference/android/support/test/uiautomator/UiSelector.html - 优点
1.xpath定位速度慢
2.uiautomator是android的工作引擎,速度快 - 缺点
1.表达式书写复杂,容易写错且IDE没有提示 - 用法
driver.find_element_by_android_uiautomator(表达式).click()
1.通过text文本定位
new UiSelector().text("text文本")
2.文本较长,可模糊匹配
new UiSelector().textContains("包含text文本")
3.以某个文本开头匹配
new UiSelector().textStartsWith("文本开头")
4.用正则表达式textMatches匹配