appium+python自动化31-android_uiautomator定位,,前言appium的前


前言

appium的前身就是封装android的uiautomator这个框架来的,所以uiautomator的一些定位方法也可以用

text

1.通过text文本定位语法

new UiSelector().text("text文本")

2.文本比较长的时候,可以用textContains模糊匹配,只要文本包含匹配内容就可以了。

new UiSelector().textContains("包含text文本")

3.textStartsWith是以某个文本开头的匹配

new UiSelector().textStartsWith("以text文本开头")

4.正则匹配textMatches,这个需要配合正则表达式,就不举例了。

new UiSelector().textMatches("正则表达式")

# coding:utf-8from appium import webdriverdesired_caps = {                'platformName': 'Android',                'deviceName': '127.0.0.1:62001',                'platformVersion': '4.4.2',                'appPackage': 'com.baidu.yuedu',                'appActivity': 'com.baidu.yuedu.splash.SplashActivity',                'noReset': 'true',                'resetKeyboard': 'true',                'unicodeKeyboard': 'true'                }driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)# 等主页面activity出现driver.wait_activity(".base.ui.MainActivity", 10)# 1.textloc_text = 'new UiSelector().text("图书")'driver.find_element_by_android_uiautomator(loc_text).click()# # 2.textContains# loc_textContains = 'new UiSelector().textContains("图")'# driver.find_element_by_android_uiautomator(loc_textContains).click()# # 3.textStartsWith# loc_textStart = 'new UiSelector().textStartsWith("图")'# driver.find_element_by_android_uiautomator(loc_textStart).click()

resourceId

1.resourceId根by_id一样

new UiSelector().resourceId("id")

# resourceId定位loc_id = 'new UiSelector().resourceId("com.baidu.yuedu:id/webbooktitle")'driver.find_element_by_android_uiautomator(loc_id).click()

技术分享图片

className

1.页面上的class属性一般不唯一,多半用在复数定位时候。比如通过class属性定位‘排行‘这个按钮下标就是2。

new UiSelector().className("className")

# className复数定位loc_class = 'new UiSelector().className("android.widget.TextView")'driver.find_elements_by_android_uiautomator(loc_class)[2].click()

技术分享图片

description

1.由于这个app的contenet-des属性都是空的,就不用代码演示了,跟上面方法一样。

new UiSelector().description("contenet-des属性")

参考代码

# coding:utf-8from appium import webdriverfrom time import sleepdesired_caps = {                'platformName': 'Android',                'deviceName': '127.0.0.1:62001',                'platformVersion': '4.4.2',                'appPackage': 'com.baidu.yuedu',                'appActivity': 'com.baidu.yuedu.splash.SplashActivity',                'noReset': 'true',                'resetKeyboard': 'true',                'unicodeKeyboard': 'true'                }driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)# 等主页面activity出现driver.wait_activity(".base.ui.MainActivity", 10)# 1.textloc_text = 'new UiSelector().text("图书")'driver.find_element_by_android_uiautomator(loc_text).click()# # 2.textContains# loc_textContains = 'new UiSelector().textContains("图")'# driver.find_element_by_android_uiautomator(loc_textContains).click()# # 3.textStartsWith# loc_textStart = 'new UiSelector().textStartsWith("图")'# driver.find_element_by_android_uiautomator(loc_textStart).click()sleep(2)# resourceId定位'小说'loc_id = 'new UiSelector().resourceId("com.baidu.yuedu:id/webbooktitle")'driver.find_element_by_android_uiautomator(loc_id).click()sleep(2)# className复数定位loc_class = 'new UiSelector().className("android.widget.TextView")'driver.find_elements_by_android_uiautomator(loc_class)[2].click()

appium+python自动化31-android_uiautomator定位

评论关闭