- appium在android的底层用的引擎是uiautomator2自动化化测试框架去驱动自动化执行的,在定位元素的时候,可以借助uiautomator2的语法实现元素的定位。
- uiautomator2的语法定位比xpath的速度快,但是书写复杂,IDE没有提示容易写错
用uiautomator2定位的话书写方式是:newUiSelector().属性名("属性值")
根据resourceId定位: | driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceid("id值")') |
根据className定位: | driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().className("class值")') |
根据content-desc定位: | driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().description("content-desc值")') |
根据text属性定位:全匹配 | driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("text属性值")') |
根据text属性定位:模糊匹配 | driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("text属性值")') |
根据text属性定位:以文本开头匹配 | driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textStartsWith("text属性值的开头一部分")') |
根据text属性定位:以正则匹配 | driver.find_element(MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textMatches("正则表达式")') |
组合定位:类名+文本 | 'new UiSelector().className("class的值").text("text属性的值"))' |
组合定位:id+文本 | 'new UiSelector().resourceId("resource-id属性的值").text("text属性的值")' |
其他属性也可以任意组合进行定位 | |