通过appium-desktophtml" title=定位>定位元素
![](https://img-blog.csdnimg.cn/img_convert/f9144a3dc6bcde4d2de55332c6f8a0f1.png)
![](https://img-blog.csdnimg.cn/img_convert/2187b91ea0dce34655789984de5ef587.png)
![](https://img-blog.csdnimg.cn/img_convert/f4e468d2dded78df6d5b901651b3099f.png)
![](https://img-blog.csdnimg.cn/img_convert/b1bbb1a69cbf00e2cd200fe0a1fcc901.png)
![](https://img-blog.csdnimg.cn/img_convert/c6262e4b0979a14aadb9664e499b00e1.png)
ClassName
Android
Android的class属性对应ClassNamehtml" title=定位>定位方式,ClassName一般都是会重复的,可以通过index来获取需要的元素。(从0开始查找dom树中的同名class属性)
iOS
iOS的type属性对应CLassNamehtml" title=定位>定位方式,ClassName一般都是会重复的,可以通过index来获取需要的元素。(从0开始查找dom树中的同名class属性)
ID
Android
Android的resource-id对应IDhtml" title=定位>定位方式,这个id也可能存在重复情况,可以通过index来获取需要的元素。(从0开始查找dom树中的同名resource-id属性)
使用appium-desktop来获取元素时,如果提示有id的html" title=定位>定位方式,则可以只接获取,代表唯一。
XPATH
Android
Android的Xpathhtml" title=定位>定位与PC的XPATHhtml" title=定位>定位大同小异,可以通过相对路径的html" title=定位>定位方式html" title=定位>定位,区别在于,这里相对路径html" title=定位>定位的//后只可以接Android的class属性或*。(//html" title=android>android.widget.Button[@text="登 录"])
iOS
iOS10 以上使用XCUITest框架后,原生框架不支持XPATH,Appium进行了转换,速度很慢不建议使用。
AccessibilityId
Android
Android的content-desc属性对应AccessibilityIdhtml" title=定位>定位方式,这个content-desc属性专门为残障人士设置,如果这个属性不为空则推荐使用。
iOS
iOS的label和name属性都对应AccessibilityIdhtml" title=定位>定位方式,如果有则推荐使用。
AndroidUIAutomator
Android的源生测试框架的html" title=定位>定位方式,html" title=定位>定位速度快。推荐使用牢记常用的几种。
打开方法:
![](https://img-blog.csdnimg.cn/img_convert/168369d88a97f3fc099f92fb3cf02345.png)
常用html" title=定位>定位方法
# 这个在运行时,调用的是Android自带的UI框架UiAutomator的Api
# 介绍几个简单常用的,text、className、resource-id
# text
# 匹配全部text文字
driver.find_element_by_html" title=android>android_uiautomator('new UiSelector().text("手机号")')
# 包含text文字
driver.find_element_by_html" title=android>android_uiautomator('new UiSelector().textContains("机")')
# 以text什么开始
driver.find_element_by_html" title=android>android_uiautomator('new UiSelector().textStartsWith("手")')
# 正则匹配text
driver.find_element_by_html" title=android>android_uiautomator('new UiSelector().textMatches("^手.*")')
# className
driver.find_elements_by_html" title=android>android_uiautomator('new UiSelector().className("html" title=android>android.widget.TextView")')
# classNameMatches
driver.find_elements_by_html" title=android>android_uiautomator('new UiSelector().classNameMatches("^html" title=android>android.widget.*")')
# resource-id、resourceIdMatches 类似我们html id 这个可能重复,
driver.find_element_by_html" title=android>android_uiautomator('new UiSelector().resourceId("com.syqy.wecash:id/et_content")') # description driver.find_element_by_html" title=android>android_uiautomator('new UiSelector().description("S 日历")') # descriptionStartsWith driver.find_element_by_html" title=android>android_uiautomator('new UiSelector().descriptionStartsWith("日历")') # descriptionMatches driver.find_element_by_html" title=android>android_uiautomator('new UiSelector().descriptionMatches(".*历$")')
#组合html" title=定位>定位
self.driver.find_element_by_html" title=android>android_uiautomator('new UiSelector().resourceId("com.xueqiu.html" title=android>android:id/tab_name").text("我的")').click()
#父子关系html" title=定位>定位
self.driver.find_element_by_html" title=android>android_uiautomator('new UiSelector().resourceId("com.xueqiu.html" title=android>android:id/title_container").childSelector(text("股票"))')
#兄弟关系html" title=定位>定位
self.driver.find_element_by_html" title=android>android_uiautomator('new UiSelector().resourceId("com.xueqiu.html" title=android>android:id/title_container").fromParent(text("股票"))')
#滚动查找
self.driver.find_element_by_html" title=android>android_uiautomator('new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("查找的元素文本").instance(0));')
iOSPredicateString
仅支持iOS10以上,可以多个属性同时html" title=定位>定位,推荐。(替代XPATH)
driver.find_elements_by_ios_predicate("label == '登录'")
driver.find_elements_by_ios_predicate("type='XCUIElementTypeOther' and name='联系人,标签, 第2个按钮,共3个'")
iOSUIAutomation
iOS9.3以下使用,现在已经废弃,iOSPredicateString代替了iOSUIAutomation