appium的元素定位总结

appium的元素定位也是继承selenium的方法并有APP独有的定位方法,包含单个元素定位、元素列表定位:

单个元素定位:

driver.find_element_by_accessibility_id(id) 
driver.find_element_by_android_uiautomator(uia_string) 
driver.find_element_by_class_name(name) 
driver.find_element_by_css_selector(css_selector) 
driver.find_element_by_id(id_) 
driver.find_element_by_ios_predicate(predicate_string) 
driver.find_element_by_ios_uiautomation(uia_string) 
driver.find_element_by_link_text(link_text) 
driver.find_element_by_name(name) 
driver.find_element_by_partial_link_text(link_text) 
driver.find_element_by_tag_name(name) 
driver.find_element_by_xpath(xpath)

元素列表定位即elements_by了。

 

部分方法案例:

1. find_element_by_id

用法:通过元素id来查找元素

示例:driver.find_element_by_id('com.android.calculator2:id/digit8')

这里元素id和 UIAutomatorView 中该元素的 resource id是一致的。

这里需要注意的是,Android 4.3 以上系统才会有 resource id。我尝试过 Android 4.2.2系统,结果resource id这里一直是空。

2. find_element_by_accessibility_id(id)

示例:driver.find_element_by_accessibility_id("delete")

这里的accessibility id 跟 UIAutomatorView 中该元素的 content-desc 是一致的。

3. find_element_by_name(name)

用法:通过元素 name 来查找元素

示例:driver.find_element_by_name("Create a new contact")

这里的name跟 UIAutomatorView 中该元素的 Text是一致的。

需要注意的是,最好不要用这种定位方法,因为 Appium v1.0 已经不建议使用通过name进行定位的方式。

4. find_element_by_class_name(name)

用法:通过元素class name来查找元素

示例:driver.find_element_by_class_name("android.widget.Button")

这里的class name跟 UIAutomatorView 中该元素的 class 是一致的。

需要注意的是,由于 class name不是唯一的,所以可能存在一个页面上有多个元素具有相同的class name。

5. find_element_by_xpath(xpath)

用法:通过元素xpath来查找元素

示例:driver.find_element_by_xpath("//android.widget.Button[@text='8']")

通过xpath进行定位的好处是,比较绝对,任何一个元素都可以通过xpath进行定位。

Xpath的自动抓取,可以用appium inspector,详见:https://www.cnblogs.com/songzhenhua/p/12913938.html

6. find_element_by_android_uiautomator(uia_string)

用法:通过 UiAutomator 中的定位来进行定位

示例:driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.android.calculator2:id/digit8")')

这里可以通过UiSelector()的多种方式进行定位,上面只是举例采用了 resourceId而已。

摘自:http://www.cnblogs.com/miniren/

posted @ 2020-12-22 10:21  liuyi1804  阅读(237)  评论(0编辑  收藏  举报