Android APP中,若存在WebView来加载H5页面使用appium进行元素定位时,需将webdrivercontext切换webviewcontext,此时才能正常的获取H5中元素步骤参考如下

1.需原生APP中,增加如下代码(Application即可):

WebView.setWebContentsDebuggingEnabled(true)

2.此时启动webdriver调用.contexts方法获得context list打印结果中会出现WEBVIEW_xxxxxxx元素,这个元素即为webviewcontext,若无步骤1中设置项,打印中只有NATIVE_APP元素

cts = driver.contexts
print(cts)
>>>>>>['NATIVE_APP', 'WEBVIEW_xxxx'] 

3.webdriver对象调用.switch_to.context(“WEBVIEW_xxxx“)方法切换webviewcontext,调用.current_context方法,可以看到打印“WEBVIEW_xxxx

ct = driver.current_context
print("current context is :" + ct)
>>>>>>WEBVIEW_xxxx

4.若调用.switch_to.context(“WEBVIEW_xxxx“)方法报类似以下错误No Chromedriver found that can automate Chrome ‘xx.xx.xx报错是因为Appium运行过程中找不到安卓设备系统自带Webviewchrome版本对应版本的Chromedriver,导致了报错。

我们需要找到与安卓系统webview对应chromedriver版本更新appium安装目录中。

步骤如下:

  1. 手机(USB)链接电脑后,切换到含有webview页面
  2. 打开电脑浏览器浏览器输入chrome://inspect/#devices
  3. 页面中会提示如图所示信息fecde6050387a6aeed2933363856fcdb.png此时可以看到当前手机chromedriver版本为81.0.4404.71
  4. 然后点击Chromedriver下载地址ChromeDriver仓库 下载最新的81.0.4404.xxx的版本即可(81.0.4404.xx可能找不到,下载81.0.4404这个版本最新的就行)
  5. 下载下来后解压拿到文件chromedriver.exe文件,将该文件添加/替换appium安装目录下的resourcesappnode_modulesappiumnode_modulesappiumchromedriverchromedriverwin,可能原目录中不存在chromedriverwin这两层目录,需手动创建
  6. 经上述步骤后即可运行成功;

参考资料Appium运行报错:No Chromedriver found that can automate Chrome ‘39.0.0’ – 凌风子 – 博客运行appium切换webview时候遇到报错:’No Chromedriver found that can automate Chrome ‘xx.xx.xx‘ 此报错是因为Apphttps://www.cnblogs.com/xioawu-blog/p/11661020.html

5.再次运行python脚本可以使用常规定位方式classname定位元素,

code

h5中定义div标签rg_form,此时可以通过如下方式定位

driver.find_element(AppiumBy.CLASS_NAME, 'rg_form')

h5中定义input标签idpassword,此时可以通过如下方式定位

driver.find_element(AppiumBy.ID, "password")

 6.步骤5在定位元素发生错误提示类似InvalidArgumentException: Message: invalid argument: invalid locator,则尝试webdriver的启动方式中增加如下参数

desired_caps["chromeOptions"] = {'w3c': False}
desired_caps["showChromedriverLog"] = True

参考资料Appium报错InvalidArgumentException: Message: invalid argument: invalid locator_想要成为酷盖但是失败小明博客-CSDN博客_invalidargumentexception使用xpath定位元素,报错:selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator。解决办法需要创建driver时增加2个参数caps[‘chromeOptions’] = {‘w3c’:False}caps[‘showChromedriverLog’] = True参考:https://github.com/appium/appium/issues/133https://blog.csdn.net/m0_38039283/article/details/113430598

7.若想再切回原生app的context,只需webdriver对象调用.switch_to.context(“NATIVE_APP”)方法进行切换即可

其他:

android项目集成mpaas框架,直接运行debug版本的app或者运行带签名debug包,就能获得WEBVIEW_xxxxxxx的context

原文地址:https://blog.csdn.net/sayhello_class/article/details/128236917

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_22978.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注