优缺点
- 优点
- xpath 定位速度慢
- UIAutomator 是 Android 的工作引擎,速度快
- 滚动查找很方便
- 缺点
- 表达式书写复杂,容易写错 IDE 没有提示
定位方式
- 通过 resource-id 定位
- 通过 classname 定位
- 通过 content-desc 定位
- 通过文本定位
- 组合定位
- 通过父子关系定位
用法
- driver.find_element_by_android_uiautomator("表达式")
- 注:外层要用单引号,内层的字符串用双引号,因为本来就是 Java,Java 双引号才表示字符串
- 通过文本定位
- new UiSelector().text("text文本")
- 通过 textContains 模糊匹配
- new UiSelector().textContains("text文本")
- 通过某个文本开头匹配
- new UiSelector().textStartWith("text文本")
- 正则表达式匹配
- new UiSelector().textMatches("text文本")
- 组合定位
- 比如 id 与 text 的属性组合:driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/login_account").text("我的")')
- 父子关系定位:childSelector,先定位到父类,再用 childSelector 来定位子类
- driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/login_account").childSelector(text("股票"))')
- 兄弟定位:fromParent
- driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/login_account").fromParent(text("股票"))')