Appium 元素定位 控件定位 uiautomatorviewer TouchAction Toast

元素定位

ID定位

Android 系统元素的 id 为 resource-id,

driver.find_element(By.ID, "android:id/text3")

content-desc 定位

content-desc 的值与 accessibility_id 的值相同

driver.find_element(MobileBy.ACCESSIBILITY_ID, "accessibility_id_1")



控件定位

App客户端的页面通过 XML 来实现 UI 的布局,XML DOM 是定义访问和操作 XML 的标准方法。可以使用 XPath 对 XML DOM 中节点的定位

XPath定位

text 属性定位

driver.find_element(MobileBy.XPATH, "//*[@text='']")

resource-id 属性定位

driver.find_element(MobileBy.XPATH, "//*[@resource-id='']")

class 属性定位

driver.find_element(MobileBy.XPATH, "//*[@class='']")

content-desc 属性定位

driver.find_element(MobileBy.XPATH, "//*[@content-desc='']")

模糊定位

driver.find_element(MobileBy.XPATH, "//*[contains(@text='取消')]")

组合定位

driver.find_element(MobileBy.XPATH, "//*[@text='搜索' and @resource-id='search']")

层级定位

driver.find_element(MobileBy.XPATH, "//*[@text='搜索'/..]")



Uiautomatorviewer

使用 Uiautomatorviewer 工具可以抓取页面的元素。
uiautomatorviewer 是 Android SDK 自带的工具,在sdk/tools/uiautomatorviewer 路径下,可以将sdk/tools 路径配置到环境变量中,即可在命令行中 输入uiautomatorviewer 快速打开

两种方式:UiSelector(),UiScrollable()

定位元素:

driver.find_element_by_android_uiautomator('new UiSelector().text("我的")').click()
driver.find_element_by_android_uiautomator('new UiSelector().textContains("帐号密码")').click()
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/login_account")').send_keys("123")

# 组合定位
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/login_account").text("登录")')

# 滚动查找
driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().'
                                                        'scrollable(true).instance(0)).'
                                                        'scrollIntoView(new UiSelector().text("查找").'
                                                        'instance(0))')



控件交互

appium 提供的 API 可操作页面及页面上的节点
常用 API 如点击 click()、输入 send_keys() 、获取元素属性 get_attribute()、获取页面源码 page_source

driver.find_element(MobileBy.XPATH, "//*[@resource-id='']").click()
driver.find_element(MobileBy.XPATH, "//*[@='']").send_keys('')
# 获取 text属性
driver.find_element(MobileBy.XPATH, "//*[@='']").get_attribute('text')
# 获取其他属性 如:class、resource-id、content-desc
driver.find_element(MobileBy.XPATH, "//*[@='']").get_attribute('class')
...
# 获取源码
driver.page_source



TouchAction

TouchAction 是 AppiumDriver 提供的一个模拟手势操作的辅助类,通过它可以对手机屏幕进行手势操作,如滑动,长按,拖动等

参考常见用法:https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/touch-actions.md

  • press
  • release
  • moveTo
  • tap
  • wait
  • longPress
  • cancel
  • perform

press

实现对元素或坐标的按下操作

press(WebElement e1)
press(int x, int y)
press(WebElement e1, int x, int y) # 在控件的左上角的 x 坐标偏移 x 单位,y坐标偏移 y 单位

release

实现在某个控件上的释放操作

release(WebElement e1)
release() # 可以直接执行释放操作

move_to

以控件为目标,从其他点移动到该控件目标上

move_to(WebElement e1)

以(x,y)点为目标,从其他点移动到该目标

move_to(WebElement e1, int x, int y)

tap

在某个控件的中心点进行敲击,或在某个坐标点进行敲击,或以控件的左上角为基准,x,y为偏移量进行敲击

tap(WebElement e1)
tap(int x, int y)
tap(WebElement e1, int x, int y)

wait

等待,单位为毫秒

wait()
wait(long timeout)
wait(long timeout, int nanos)

longpress

长按,参数有:e1,x,y,duration(持续时间)

long_press(WebElement e1)
long_press(int x, int y)
long_press(WebElement e1, int x, int y)

cancel

取消动作

cancel()

perform

执行操作

perform()

Toast

driver.find_element_by_xpath("//*[@class='android.widget.Toast']")
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值