appium 处理webview

打开webview页面 

chrome://inspect/#devices

 获取webview driver版本

1、

2、

3、

chenshifengdeMacBook-Pro:ChromeDriver chenshifeng$ adb shell pm list package|grep webview
package:com.android.webview
package:org.chromium.webview_shell
chenshifengdeMacBook-Pro:ChromeDriver chenshifeng$ adb shell pm dump com.android.webview|grep version
      versionCode=373018615 minSdk=21 targetSdk=29
      versionName=74.0.3729.186

 切换native与webview

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:test_webview.py
@time:2020/11/29
"""

from appium import webdriver


class TestWebview:
    def setup(self):
        desired_caps = {
            "platformName": "Android",
            "platformVersion": "8.0",
            "automationName": "UiAutomator2",
            "deviceName": "emulator-5554",
            "appPackage": "com.example.android.apis",
            "appActivity": "com.example.android.apis.ApiDemos",
            "chromedriverExecutable": "/Users/chenshifeng/Library/SoftWare/ChromeDriver/chromedriver74",
            # "appActivity": ".common.MainActivity",
            "noReset": True,  # 不重置APP
            # "autoGrantPermissions": True,
            # "skipServerInstallation": True,  # 跳过 uiAutomator2服务器安装
            # "dontStopAppOnReset": True,
            # "skipDeviceInitialization": True,  # 跳过设备初始化
            # "unicodeKeyboard": True,  # 默认启用 Unicode 输入
            # "resetKeyboard": True,  # 与上面合用,可以输入中文
            # "newCommandTimeout": 300
        }
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
        self.driver.implicitly_wait(10)

    def teardown(self):
        self.driver.quit()

    def test_webview(self):
        self.driver.find_element_by_xpath('//*[contains(@text,"Views")]').click()
        print(self.driver.contexts)

        self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().'
                                                        'scrollable(true).instance(0)).'
                                                        'scrollIntoView(new UiSelector().text("WebView").'
                                                        'instance(0));').click()
 

        # self.driver.find_element_by_xpath('//*[contains(@text,"Hello World! - 1")]').click()  # 可以与native页面一样处理,但这种方法由于不同的手机渲染的结果不一样,不具有通用性
        print(self.driver.contexts)  # 获取上下文
        self.driver.switch_to.context(self.driver.contexts[-1]) 进入到webview页面
        self.driver.find_element_by_xpath('/html/body/a').click()

        # print(self.driver.page_source)
        sleep(5)

执行结果

============================== 1 passed in 32.47s ==============================

Process finished with exit code 0
PASSED                        [100%]['NATIVE_APP']
['NATIVE_APP', 'WEBVIEW_com.example.android.apis']
原文地址:https://www.cnblogs.com/feng0815/p/14056127.html