简介
在做UI自动化的时候,有很大一部分精力是在进行定位元素操作,元素定位如果不准确,直接影响自动化的成败和效率
环境
- Appium server :v1.20.2
- Appium-Python-Client :2.1.2
- selenium 4.1.0
常用的元素定位方式
- id定位元素
- class_name定位元素
- content-desc定位元素
name 定位元素(appium1.5及之后的版本废弃了name属性)- xpath定位元素
- uiautomator定位元素,Android独有
id 定位
-
使用方法
find_element(AppiumBy.ID, “”)
driver.find_element_by_id() 已过时 -
元素的resource-id
-
唯一标识该元素的值(id有时候并不唯一)

-
示例:一般优先使用此方式定位
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/actionbar_item_search")
class_name定位
- 使用方法
find_element(AppiumBy.CLASS_NAME, “”) - 此方法一般不使用,由于界面元素的class重复度太高,基本上不存在唯一值
- 元素的class属性

- 示例:
driver.find_element(AppiumBy.CLASS_NAME, "android.widget.LinearLayout")
content-desc定位
- 使用方法
find_element(AppiumBy.ACCESSIBILITY_ID, “”) - 元素的content-desc属性
content-desc属性是用来描述该元素的作用,一般都是为空

- 示例
find_element(AppiumBy.ACCESSIBILITY_ID, "")
name定位
- 此方法在Appium1.5及之后的版本废弃了name属性
xpath 定位
单个元素定位
-
根据id属性定位
表达式://*[@resource-id=’id属性’] -
根据text属性定位
表达式://*[@text=’text文本属性’] -
根据class属性定位
表达式: //*[@class=’class属性’] -
通过content-desc属性定位
表达式: //*[@content-desc=’desc的文本’] -
示例
driver.find_element(AppiumBy.XPATH, "//*[@resource-id='com.netease.edu.study:id/look_more']").click() driver.find_element(AppiumBy.XPATH, "//*[@text='查看更多']").click() driver.find_element(AppiumBy.XPATH, "//*[@class='android.widget.TextView']").click() driver.find_element(AppiumBy.XPATH, "//*[@content-desc='更多按钮']").click()
模糊定位
-
contains是模糊匹配的定位方法,对于一些元素的属性是可变的,但有一部分是固定的,这种就可以模糊匹配
//[contains(@text, ‘查看’)]
//[contains(@content-desc, ‘’)]
//[contains(@resource-id, ‘id属性’)]
//[contains(@clsss, ‘class属性’)]
组合定位
当单个元素定位不唯一时,这时候我们就可以采用多个属性组合定位
-
如果元素有多个属性,通过xpath也可以同时匹配2个属性,text, resource-id,class ,index,content-desc这些属性都能任意组合定位
id_class = ‘//android.widget.TextView[@resource-id=“com.netease.edu.study:id/look_more”]’
desc_class = ‘//*[@text=“查看更多” and @index=“2”]’
层级定位
有时候我们遇见元素属性除了class属性,其他属性都为空,且class属性又不唯一,这时候一般方法就没办法进行定位了
- 父亲定位儿子
‘//[@resoure-id=“com.netease.edu.study:id/composite_item_list_recyclerview”]/android.widget.TextView’
'//[@resoure-id=“com.netease.edu.study:id/composite_item_list_recyclerview”]/com.netease.edu.study:id/item_kingkong_area_image[2]’ - 儿子定位父亲
‘//[@resource-id=“com.netease.edu.study:id/composite_item_list_recyclerview”]/…’
'//[@resource-id=“com.netease.edu.study:id/composite_item_list_recyclerview”]/parent::’
'//[@resource-id=“com.netease.edu.study:id/composite_item_list_recyclerview”]/parent::android.widget.LinearLayout’ - 兄弟元素定位
‘//*[@resource-id=“com.netease.edu.study:id/composite_item_list_recyclerview”]/…/android.widget.RelativeLayout’
uiautomator定位
说明
Appium就是封装android的uiautomator这个框架来的,所以uiautomator的一些定位方法正常使用即可
官网地址:https://developer.android.google.cn/reference/androidx/test/uiautomator/UiSelector
使用方法
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, "new UiSelector().resourceId(\"com.netease.edu.study:id/look_more\")")
可以将:new UiSelector() 省略
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,"resourceId(\"com.netease.edu.study:id/look_more\")")
resourceId
1.通过resourceId定位
new UiSelector().resourceId("id")
2.通过resourceIdMatches正则表达式
new UiSelector().resourceIdMatches("正则表达式")
className
1.通过className定位
new UiSelector().className("className")
2.通过classNameMatches正则表达式
new UiSelector().classNameMatches("正则表达式")
text
1.通过text文本定位语法
new UiSelector().text("text文本")
2.文本比较长或者可变,可以用textContains模糊匹配,只要文本包含匹配内容。
new UiSelector().textContains("包含text文本")
3.textStartsWith是以某个文本开头的匹配
new UiSelector().textStartsWith("以text文本开头")
4.正则匹配textMatches,这个需要配合正则表达式。
new UiSelector().textMatches("正则表达式")
content-des
1.通过content-des文本定位语法
new UiSelector().description("content-des属性")
2.文本比较长或者可变,可以用descriptionContains模糊匹配,只要文本包含匹配内容。
new UiSelector().descriptionContains("包含content-des属性文本")
3.descriptionStartsWith是以某个文本开头的匹配
new UiSelector().descriptionStartsWith("以content-des属性文本开头")
4.正则匹配descriptionMatches,这个需要配合正则表达式。
new UiSelector().descriptionMatches("正则表达式")
组合定位
1.大部分都是使用用id,class,text这三个属性进行组合,其次description这个属性也可以一起两两组合
new UiSelector().resourceId("id").text("text文本")
2.父子定位childSelector
new UiSelector().resourceId("id").childSelector(text("text文本"))
3.兄弟定位fromParent
new UiSelector().resourceId("id").fromParent(text("text文本"))
以上为内容纯属个人理解,如有不足,欢迎各位大神指正!
如果觉得文章不错,欢迎关注微信公众号,微信公众号定期推送相关测试技术文章



2081

被折叠的 条评论
为什么被折叠?



