1. Understanding WebView
Example Description:
When you open Baidu read APP→VIP full station to ads → with the UI automator to locate the elements inside,
No matter you go to the "rule details" "open" and so on, will not be able to locate, can only display a whole page, this is WebView
Note:
① can be understood to be similar to an IFRAME in selenium.
② has a clear indication of WebView in the right-hand position.
③ after clicking on a link, there is a progress bar after loading the page is generally webview. In other words, it's a H5 page.
④ui Automator can't locate the element inside.
Treatment Method 1:
① execution Print (driver.contexts) gets all the contexts
② is switching to his webview (similar to the switch window in selenium)
③ gets to his WebView source, opens in another browser, gets his XPath path, or other location method
#Coding:utf-8 fromAppiumImportWebdriverImportTimeu" "Chi Line train tickets, WebView page locator" "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 = "my"]'). Click () time.sleep (3) Driver.find_element_by_xpath ('//*[@text = "Product opinion"]'). Click () time.sleep (3)Print(driver.contexts) Driver._switch_to.context ('Webview_com.yipiao')Print('Switch Success') 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 (), ' "Rob Ticket" can I grab a ticket?)]. Click ()Driver.find_element_by_xpath (".//*[@id = ' container ']/div/div/section[2]/ul/li[2]/a/div[1]/span"). Click ()
Treatment Method 2:
The main thing is that you have determined that he is a webview, but the print all context returned list is only Native_app and cannot be toggled.
Do not switch, as the app is native, only need to navigate to an element on the line
This situation is not recommended to get the source code, not much use.
#Coding:utf-8 fromAppiumImportWebdriverImportTimeu" "Baidu Reading, WebView page positioning" "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 ("Rule Details"). Click ()
The WebView treatment of Python+appium learning article