appium+python自動化28-name定位

來源:互聯網
上載者:User

標籤:1.5   報錯   common   4.4   包含   組合   文法   UI   通過   

前言

appium1.5以下老的版本是可以通過name定位的,新版本從1.5以後都不支援name定位了

name定位報錯

1.最新版appium V1.7用name定位,報錯:

selenium.common.exceptions.InvalidSelectorException: Message: Locator Strategy ‘name‘ is not supported for this session

2.這個報錯是說name這個定位方法目前已經不支援了,因為appium從1.5版本開始就已經拋棄了name這種定位方法了。

xpath定位

1.既然name定位拋棄了,那就說明有更先進的定位代替了它,事實上xpath定位裡面已經包含了name這種定位方法。

2.平常用過selenium,在定位頁面上文本的時候,應該知道這個xpath文法:

//*[text()='頁面text文本']

appium裡面的xpath文法跟selenium有一點點區別:

//*[@text='頁面text文本']
text屬性

1.上面的xpath文法適合頁面上這個text屬性是唯一的,才好直接定位到,那麼問題來了:如果頁面上有多個text屬性的文本一樣,怎麼辦?

2.xpath文法裡面*是代表匹配任意的值,在selenium裡面*是匹配任意標籤,appium裡的*是匹配任意class名稱,如果幾個文本的class屬性不一樣,就可以通過以下組合:

//android.widget.TextView[@text='小說']

參考代碼
# 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'                }driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)# 擷取當前介面activityac = driver.current_activityprint(ac)# 等首頁面activity出現driver.wait_activity(".base.ui.MainActivity", 10)# 點'知道了'driver.find_element_by_id("com.baidu.yuedu:id/positive").click()sleep(2)# 定位頁面上text屬性# driver.find_element_by_name("小說").click()  # V1.5以後版本不支援name定位了driver.find_element_by_xpath("//*[@text='小說']").click()

appium+python自動化28-name定位

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.