The WebView treatment of Python+appium learning article

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.