app ui自动化--4、元素定位

目录

Appium Inspector 页面简介

通过AppiumBy方式定位

元素定位方式

ID 方式定位

python代码示例

Class Name方式定位

python代码示例

ACCESSIBILITY_ID方式定位

python代码示例

XPATH方式定位

常规定位

python代码示例

contains模糊定位

python代码示例

and组合定位

python代码示例

层级定位

通过ANDROID_UIAUTOMATOR方式定位

resource-id定位

text定位

Class Name定位

组合定位

层级定位

滑动屏幕查找元素

python代码示例


Appium Inspector 页面简介

由下图可见,通过Appium Inspector 启动APP页面后,可纵向分为三部分,横向分为两部分。

纵向最左侧:展示被测APP的当前页面

纵向中间:展示当前页面APP的代码结构

纵向最右侧:展示当前页面中,选中的元素类型和元素值,定位元素即通过这里

横向 顶部左侧 菜单(文件、查看、帮助)

横向 中间 操作栏(原生APP模式、Web/混合应用程序模式、选择元素、滑动坐标、点击坐标、返回、刷新屏幕、录制、搜索元素、将 XML 源复制到剪贴板、退出会话)

通过AppiumBy方式定位

需要导入以下库

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy

方法示例

driver.find_element(AppiumBy.xxx,'xxx')

元素定位方式

ID 方式定位

对应的是resource-id 属性,该属性是唯一的,若存在该属性时,则优先选取其作为定位方式

python代码示例


# 存在一个元素,使用.find_element接收
btn_publish = self.driver.find_element(AppiumBy.ID,'com.ss.android.article.news:id/e6l')  # 找到发布按钮
btn_publish.click()  # 点击发布按钮

Class Name方式定位

通过该方式定位通常会返回多个元素,如下图中“北京”是隶属于android.widget.Button的class属性中,但是index为5,就说明有同一个class,返回了多个元素,所以在使用时,我们需要指定元素的索引

python代码示例

# 存在多个元素,使用.find_elements接收
navigation = self.driver.find_elementsAppiumBy.CLASS_NAME,'android.widget.Button')

# 使用元素,通过下标获取
navigation[5].click()

优化的写法:增加了显示等待


from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

navigation = 'android.widget.Button'
WebDriverWait(self.driver,10,1).until(EC.visibility_of_all_elements_located((By.CLASS_NAME,navigation)))
self.driver.find_elements(by=By.CLASS_NAME, value=navigation)[5].click()

ACCESSIBILITY_ID方式定位

AccessibilityId也称为content-desc

python代码示例

discover = self.driver.find_element(AppiumBy.ACCESSIBILITY_ID,'发现')
discover.click()

XPATH方式定位

常规定位

需要写成xpath表达式,来定位内容,如下图所示,定位text的内容

若不确定xpath表达式是否能正确的找到唯一元素,可提前通过图中搜索元素的按钮去验证, 步骤如图所示。

可以看到在当前页面中,找到了唯一的一个元素。

python代码示例

btn_publish = self.driver.find_element(AppiumBy.XPATH, '//*[@text="发布"]') # 找到发布按钮
btn_publish.click() # 点击发布按钮

# 以下两种查找方式都可以
//*[@text="发布"]
//android.widget.TextView[@text="发布"]

contains模糊定位

该方式经常用于获取toast的时候,toast文本内容较长,可以采用contains包含部分文本的匹配方式。当然,也可以用来模糊匹配文本属性,如下图

python代码示例

btn_search = self.driver.find_element(AppiumBy.XPATH, '//*[contains(@text,"两会热点话题")]') # 找到搜索输入框
btn_search.click() # 点击搜索输入框

and组合定位

例如,查找某一元素,需要用到两个及以上的元素组合,才能获取到唯一的元素,则可以使用这种方式

python代码示例

btn_video = self.driver.find_element(AppiumBy.XPATH, '//*[@class="android.widget.TextView" and @text="视频"]') # 找到视频按钮
btn_video.click() # 点击视频按钮

层级定位

该方式定位,属于xpath全路径形式,也是层级形式,例如下图标记部分,就是通过xpath全路径查找

通过ANDROID_UIAUTOMATOR方式定位

android uiautomator原理是通过android 自带的android uiautomator的类库去查找元素,和appium的定位差不多,或者说它比appium的定位方式更多以及更适用,它也支持id、className、text、模糊匹配等方式定位。

resource-id定位


# 绝对定位
f = self.driver.find_elements(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("com.ss.android.article.news:id/crk")')
f[2].click()

# 正则定位

text定位


# text绝对定位
a = self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'new UiSelector().text("未登录")')
a.click()

# text模糊定位
b = self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'new UiSelector().textContains("消息")')
b.click()

# text根据开头的文本定位
c = self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textStartsWith("立即")')
c.click()

# text正则定位
d = self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textMatches(".*实现目标.*")')
d.click()

# text正则定位
e = self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textMatches("^奋斗向未来.*")')
e.click()

Class Name定位


# 绝对定位
g = self.driver.find_elements(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().className("android.widget.Button")')
g[4].click()

# 正则定位

组合定位


#组合定位
self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/tab_name").text("我的")').click()

# 组合定位,一般组合用id,class,text这三个属性会比较好一点
# id+class 属性组合
id_class = 'resourceId("com.xyh.commerce:id/ll_personal").className("android.widget.LinearLayout")'
driver.find_element_by_android_uiautomator(id_class).click()

层级定位


#父子关系定位
self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/title_container").childSelector(text("股票"))')

#兄弟关系定位
self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/title_container").fromParent(text("股票"))')

滑动屏幕查找元素

该方式在现实测试中会是比较常见的查找元素的方式,使用By.ANDROID_UIAUTOMATOR方法,原理是:一直滑动屏幕,去寻找指定的元素

python代码示例

# 测试示例
test = self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,
                                                   "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"查找的元素文本\").instance(0))")
test.click()

# 找地区
inr_country = self.driver.find_element(By.ANDROID_UIAUTOMATOR,
                                                   "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"印度尼西亚\").instance(0))")
inr_country.click()

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值