Python+Appium學習篇之WebView處理

來源:互聯網
上載者:User

標籤:oid   main   說明   ext   成功   bsp   div   dev   sha   

1.認識WebView

執行個體說明:

當你開啟百度閱讀APP→VIP全站去廣告→用內建的 UI Automator去定位裡面的元素,

不管你去定位  ‘規則詳情‘  ‘開通‘等等,都會定位不到,只能顯示一個整體頁面,這個就是WebView

Note:

① 可以理解與selenium裡的iframe類似

②在右邊定位裡有明確的表示是 WebView

③點擊一個連結後,有進度條載入後頁面一般都是Webview.  也就是說,是一個H5頁面了

④UI Automator 不能定位到裡面的元素

處理方法1:

①執行  print(driver.contexts)  擷取所有的上下文

②在切換到他的webview裡面去(類似selenium裡的切換視窗)

③擷取到他的webview源碼,在其他瀏覽器開啟,擷取他的xpath路徑,或者其他定位方法

#coding:utf-8from appium import webdriverimport timeu‘‘‘智行火車票,webview頁面定位‘‘‘desired_caps = {‘platformName‘: ‘Android‘,                ‘deviceName‘: ‘9a762346‘,                ‘platformVersion‘: ‘6.0.1‘,                ‘noReset‘: True,                ‘appPackage‘: ‘com.yipiao‘,                ‘appActivity‘: ‘com.zt.main.entrance.ZTLaunchActivity‘}driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘, desired_caps)time.sleep(10)print(driver.context)driver.find_element_by_xpath(‘//*[@text="我的"]‘).click()time.sleep(3)driver.find_element_by_xpath(‘//*[@text="產品意見"]‘).click()time.sleep(3)print(driver.contexts)driver._switch_to.context(‘WEBVIEW_com.yipiao‘)print(‘切換成功‘)p=driver.page_sourcewith open(‘1111.html‘,‘wb‘) as f:    f.write(p.encode(‘utf-8‘))time.sleep(2)#driver.find_element_by_xpath("//*[contains(text(),‘【搶票】我能搶到票嗎‘)]").click()driver.find_element_by_xpath(".//*[@id=‘container‘]/div/div/section[2]/ul/li[2]/a/div[1]/span").click()

處理方法2:

主要是你已經確定他是一個webview,但是列印全部上下文返回的list裡只有NATIVE_APP,無法進行切換。

就不要去切換了,當做是APP原生的,只需要定位到某個元素就行

這種情況不建議用擷取源碼,沒太大用。

   

#coding:utf-8from appium import webdriverimport timeu‘‘‘百度閱讀,webview頁面定位‘‘‘desired_caps = {"platformName": "Android",                "deviceName": "9a762346",                "platformVersion": "6.0.1",                "noReset": True,                "appPackage": "com.baidu.yuedu",                "appActivity": "com.baidu.yuedu.splash.SplashActivity"}driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘, desired_caps)time.sleep(10)driver.find_element_by_accessibility_id(‘VIP‘).click()time.sleep(3)driver.find_element_by_accessibility_id("規則詳情").click()

 

Python+Appium學習篇之WebView處理

相關文章

聯繫我們

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