標籤:eid python 文法 android 方法 按鈕 com nts esc
前言
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,這個需要配合Regex,就不舉例了。
new UiSelector().textMatches("Regex")
# 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定位