Appium定位元素的方法有不少,好比常常用的by_name、by_resource-id、by_classnameandroid
下面介紹下經過UI Automator方法來定位元素的方法,並且這些方法定位都比較準。web
先假設咱們經過UiAutomator定位到的元素部分屬性以下:
Index 1
Text xxx
Resource-id android:id/text1
Class android.widget.TextView
Content-desc xxxide
1、 經過Text屬性定位ui
直接經過text名稱定位:
Driver.find_element_by_android_uiautotmator(‘new UiSelector().text(「xxx」)’)element
查找某個字符串包含有xxx的元素
Driver.find_element_by_android_uiautomator(‘new UiSelector().textContains(「xxx」)’)字符串
某個以x打頭的元素
Driver.find_element_by_android_uiautomator(‘new UiSelector().textStartWith(「x」)’)get
經過Matches方法查找
Driver.find_element_by_android_uiautomator(‘new UiSelector().textMatches(「^xx.*」)’)it
2、 經過class屬性定位class
經過class名稱查找:
driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.TextView").text("xxx")') List
匹配text是xxx而且classname含有TextView的元素:
driver.find_element_by_android_uiautomator('new UiSelector().classNameMatches(".*TextView$").text("xxx")')
3、 Xpath方法定位
經過同級元素定位同級元素,yyyy爲同級元素節點text名稱
driver.find_element_by_android_uiautomator('new UiSelector().text("xxx").fromParent(new UiSelector().text("yyyy"))')
經過父級元素定位子元素 ListView爲父級節點classname
driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.ListView").childSelector(new UiSelector().text("xxx"))')
4、 resourceId屬性定位
經過resourceId定位:
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("android:id/text1")')
經過matches方法定位:
driver.find_element_by_android_uiautomator('new UiSelector().resourceIdMatches(".*id/text1$")')
好了,先介紹不少吧,對元素進行操做的一些方法及獲取元素的屬性值都與web的相似。