Appium之切换Native和WebView

1、关于Native?

native是使用原生系统内核的,相当于直接在系统上操作。是我们传统意义上的软件,更加稳定。Native_App即原生APP开发模式,利用iOS、Android开发平台官方提供的开发工具进行APP的开发。

2、关于WebView?

WebView是一个基于webkit引擎、展现web页面的控件,它的作用是用来展示一个web页面。

Android的Webview在低版本和高版本采用了不同的webkit版本内核,Android4.4+版本直接使用了Chrome作为内置网页浏览器。

现在大部分app都是混合式的native+webview,即Hybrid_App(混合应用)。

使用weditor工具,获取到小猿搜题app-》英语作文-》优秀作文-》打开其中一篇,得到的webview如下:

但通过脚本运行却找不到WebView,源码如下:

from appium import webdriver
from time import sleep

desired_caps = {
    'autoLaunch': "True",
    'platformName': 'Android',
    'deivceName': 'Android 10.0',
    'platformVersion': '10.0',
    'appPackage': 'com.fenbi.android.solar',
    'appActivity': 'com.fenbi.android.solar.activity.RouterActivity',
    'noReset': 'True',
}

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# driver.find_element_by_xpath('//*[@resource-id="com.fenbi.android.solar:id/bottom_bar_viewpager"]/android.widget.LinearLayout[1]/android.widget.LinearLayout[2]/android.widget.ImageView[1]').click()
driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith("英语作文")').click()
driver.find_element_by_xpath('//*[@resource-id="com.fenbi.android.solar:id/grid_view"]/android.widget.LinearLayout[2]/android.widget.ImageView[1]').click()
driver.find_element_by_xpath('//android.widget.ListView/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]').click()
print(driver.contexts)

运行结果:
['NATIVE_APP']

网上查询得知:

该app未开启WebView远程调试功能(实际中可由开发人员增加WebView调试功能后重新打包apk)。

 

3、通过driver.switch_to.context() 进行Native和WebView切换?

3.1 查看手机内置WebView版本:

方法1:手机上使用Chrome浏览器里打开:https://liulanmi.com/labs/core.html

方法2:cmd执行命令:adb shell am start -a android.intent.action.VIEW -d https://liulanmi.com/labs/core.html

方法3:电脑上使用Chrome浏览器上打开:chrome://inspect/#devices,查看Devices中显示的手机内置WebView版本号(即手机中已安装Chrome浏览器的版本号)

3.2 保证Appium路径下的chromerdriver驱动与手机内置WebView版本匹配:

(1)查看chromedriver.exe版本:

进入chromedriver路径:D:Appium ode_modulesappium ode_modulesappium-chromedriverchromedriverwin,双击chromedriver.exe

(2)进入https://chromedriver.storage.googleapis.com/index.html,根据版本对应关系,下载对应版本的chromedriver,替换chromedriver路径下的chromedriver.exe程序

from appium import webdriver
from time import sleep

desired_caps = {
    'autoLaunch': "True",
    'platformName': 'Android',
    'deivceName': 'Android 10.0',
    'platformVersion': '10.0',
    'appPackage': 'com.fenbi.android.solar',
    'appActivity': 'com.fenbi.android.solar.activity.RouterActivity',
    'noReset': 'True',
    'chromedriverExecutable': r'D:Appium
ode_modulesappium
ode_modulesappium-chromedriverchromedriverwinchromedriver.exe',  # 指定Chromedriver.exr的路径
}

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
sleep(5)
# driver.find_element_by_xpath('//*[@resource-id="com.fenbi.android.solar:id/bottom_bar_viewpager"]/android.widget.LinearLayout[1]/android.widget.LinearLayout[2]/android.widget.ImageView[1]').click()
driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith("英语作文")').click()
sleep(2)
driver.find_element_by_xpath('//*[@resource-id="com.fenbi.android.solar:id/grid_view"]/android.widget.LinearLayout[2]/android.widget.ImageView[1]').click()
sleep(2)
driver.find_element_by_xpath('//android.widget.ListView/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]').click()
print(driver.contexts)
# print(driver.current_context)
driver.switch_to.context("WEBVIEW_chrome")    # driver.switch_to.context()切换到WebView
print(driver.current_context)
# contents = driver.find_elements_by_class_name("android.view.View")
# for content in contents:
#     print(content.get_attribute("text"))
print(driver.page_source)


运行结果:
['NATIVE_APP', 'WEBVIEW_chrome']
WEBVIEW_chrome
<html><head></head><body></body></html

分享几篇关于Appium WebView调试方法:

https://www.cnblogs.com/wmhuang/p/7396150.html

https://www.cnblogs.com/sao-fox/p/6396984.html

http://www.xuetimes.com/archives/1167

原文地址:https://www.cnblogs.com/Maruying/p/13618337.html