Python+Appium學習篇之WebView處理
阿新 • • 發佈:2018-07-21
tomat sha view源碼 cti contexts and not code acc
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-8 from appium import webdriver import time u‘‘‘智行火車票,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_source with 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-8 from appium import webdriver import time u‘‘‘百度閱讀,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處理