Appium+python (3) 元素定位(1)

來源:互聯網
上載者:User

標籤:span   inf   就是   code   cfs   xpath   hub   path   splash   

開啟問價夾下面的uiautomatorviewer:

 

 

    夜神模擬器裡的App後,回到uiautomatorviewer:

 

點擊左上方的Device Screenshot,這時你的夜神模擬器頁面就會顯示在這裡,右側就是對應的定位資訊了,我們主要依靠右下角的Node Detail資訊進行定位。

 

        如果你有selenium的基礎,那接下來介紹的基本一致,只是換了個方法而已:

  • text

    find_element_by_name(u"視頻")
  • id

    find_element_by_resource-id("")
  • class name

使用className獲得的view一般不止一個,所以需要遍曆得到的views,然後縮寫搜尋條件擷取目標控制項

# 擷取一個className下的所有控制項
buttons = driver.find_elements_by_class_name("")
# 點擊第一個元素
buttons.pop(0).click()
# 點擊最後一個元素
buttons.pop().click()buttons.pop(-1).click()
  • accessibility id

這個方法屬於appium擴充的定位方法

中的content-desc對應的就是accessibility_id

find_element_by_accessibility_id("")

- xpath

find_element_by_xpath("")

android uiautomator

# 注意,text裡面必須是字串,也就是說不能加u
find_element_by_android_uiautomator(‘new UiSelector().text("娛樂")‘)

此外,還有

find_element_by_link_text()find_element_by_partial_link_text()find_element_by_tag_name()

看一段代碼:

# coding: utf-8

from appium import webdriver
from time import sleepdesired_caps = {‘platformName‘: ‘Android‘,
               ‘deviceName‘: ‘127.0.0.1:62001‘,
               ‘platformVersion‘: ‘23‘,
               ‘appPackage‘: ‘com.tencent.news‘,
               ‘appActivity‘: ‘com.tencent.news.activity.SplashActivity‘,
               ‘unicodeKeyboard‘: True,
               ‘resetKeyboard‘: True}driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
# 跳過廣告
sleep(5)
# 用text定位-點擊視頻
driver.find_element_by_name(u"視頻").click()
# 點擊娛樂
driver.find_element_by_android_uiautomator(‘new UiSelector().text("娛樂")‘).click()

        上面的代碼有兩種定位方式,其實之前寫了好多種,無奈它去亂點,根本不是想要定位的位置,比如我用class定位:

        但是那一排的class是相同的,而且不能用find_elements_by_className()加索引的方式進行定位。大家也看到了,我的這個不知道為什麼text就是不顯示,似乎是應該可以顯示的,不知道問題出在哪裡

        定位方式還有一個xpath,我也很關心這個該怎麼用,但是我現在也不知道,不過似乎上面這些已經差不多可以進行用例編寫了。至於xpath,看到好多網上說有點不好用,管他呢,大家稍安勿躁,下一節我們再來介紹xpath。

 

公眾號搜尋“自動化測試實戰”或掃描下方二維碼添加關注~~~

Appium+python (3) 元素定位(1)

相關文章

聯繫我們

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