Appium-安卓原生-常見元素定位API的Python用法(二)__Python

來源:互聯網
上載者:User

安卓原生頁面常用元素定位方法的Python使用,代碼示範使用的嗶哩嗶哩安卓用戶端,模擬器是夜神模擬器,嘗試了最常用的id,class,name,xpath定位方式,還有一些其他的定位方式是webview特有的,持續更新ing

#coding=utf-8'''Created on 2017/12/24 0024@author:Changge'''from appium import webdriverimport timedesired_caps = {    'platformName':'Android',    'deviceName':'127.0.0.1:62001',    'platformVersion':'4.4.2',    'appPackage':'tv.danmaku.bili',    'appActivity':'tv.danmaku.bili.ui.splash.SplashActivity',    'unicodeKeyboard':True,    'resetKeyboard':True}driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)##休眠5s等待頁面載入完成time.sleep(5)'''各種定位方法嘗試'''#id定位,通過resource-id屬性#id單數定位,id唯一id_ele = driver.find_element_by_id('tv.danmaku.bili:id/largeLabel')print("id單數定位:" + id_ele.text)#id複數定位id_eles = driver.find_elements_by_id('tv.danmaku.bili:id/tab_title')texts = []for ele in id_eles:    texts.append(ele.text)print("id複數定位:" + str(texts))#class單數定位# class_ele = driver.find_element_by_class_name('android.support.v7.widget.RecyclerView')# print('class單數定位:'+class_ele.get_attribute('class'))#class複數定位class_eles = driver.find_elements_by_class_name('android.widget.TextView')texts = []for ele in class_eles:    texts.append(ele.text)print('class複數定位:'+str(texts))#name單數定位,通過text屬性name_ele = driver.find_element_by_name('推薦')print('name單數定位:'+name_ele.text)#name複數定位name_eles = driver.find_elements_by_name('編輯精選')texts = []for ele in name_eles:    texts.append(ele.text)print('name複數定位:'+str(texts))#xpath單數定位xpath_ele = driver.find_element_by_xpath('//android.widget.TextView[contains(@resource-id,"tv.danmaku.bili:id/ranks")]')print('xpath單數定位:'+xpath_ele.text)#xpath複數定位xpath_eles = driver.find_elements_by_xpath('//android.widget.TextView')texts = []for ele in xpath_eles:    texts.append(ele.text)print('xpath複數定位:'+str(texts))driver.quit()
運行結果

聯繫我們

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