前言
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-8
from appium import webdriver
desired_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.text
loc_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-8
from appium import webdriver
from time import sleep
desired_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.text
loc_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 QQ群交流:330467341
