appium+python自動化31-android_uiautomator定位

來源:互聯網
上載者:User

標籤: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定位

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.