1.使用id定位元素
resource-id代表id属性,使用方法:
driver.findElement(By.id("com.android.calculator2:id/digit5")).click();//点击5
2.通过name定位元素
text属性是name
driver.findElement(By.name("百度一下或输入网址"));
3.通过ClassName定位元素
class属性是classname
driver.findElement(By.ClassName("android.widget.TextView"));
4.通过xpath定位
//*[@resource-id='com.xueqiu.android:id/user_profile_icon' and @class='android.widget.ImageView']
//*[contains(@resource-id, 'user_profile_icon') and contains(@class, 'Image')]
//*[@text='基金' and contains(@resource-id, 'button')]
//*[@text='基金' and @index='1']
(//*[@text='基金'])[1]
//[contains(@resource-id, 'buttons_container')]//[@text='基金']
5.AccessibilityId定位
Android
Android的content-desc属性对应AccessibilityId定位方式,这个content-desc属性专门为残障人士设置,如果这个属性不为空则推荐使用。
iOS
iOS的label和name属性都对应AccessibilityId定位方式,如果有则推荐使用。
6.AndroidUIAutomator定位:Android的源生测试框架的定位方式,定位速度快。推荐使用牢记常用的几种。
# 这个在运行时,调用的是Android自带的UI框架UiAutomator的Api
# 介绍几个简单常用的,text、className、resource-id
# text
# 匹配全部text文字
driver.find_element_by_android_uiautomator('new UiSelector().text("手机号")')
# 包含text文字
driver.find_element_by_android_uiautomator('new UiSelector().textContains("机")')
# 以text什么开始
driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith("手")')
# 正则匹配text
driver.find_element_by_android_uiautomator('new UiSelector().textMatches("^手.*")')
# className
driver.find_elements_by_android_uiautomator('new UiSelector().className("android.widget.TextView")')
# classNameMatches
driver.find_elements_by_android_uiautomator('new UiSelector().classNameMatches("^android.widget.*")')
# resource-id、resourceIdMatches 类似我们html id 这个可能重复,
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.syqy.wecash:id/et_content")') # description driver.find_element_by_android_uiautomator('new UiSelector().description("S 日历")') # descriptionStartsWith
driver.find_element_by_android_uiautomator('new UiSelector().descriptionStartsWith("日历")') # descriptionMatches driver.find_element_by_android_uiautomator('new UiSelector().descriptionMatches(".*历$")')
7.webview定位
//获得所有contexts即上下文
driver.contexts
//查看当前的context
driver.current_context
//切换context,再用selenium常用定位方式,定位
driver.switch_to.context("WEBVIEW_com.wuba.zhuanzhuan")
appium-定位
猜你喜欢
转载自www.cnblogs.com/an5456/p/11423465.html
今日推荐
周排行