Appium Android+Appium 点击 webview 当中的元素时,提示找不到元素

我喜欢的K17 · 2019年12月04日 · 最后由 troubl_2014 回复于 2021年09月16日 · 4099 次阅读

问题描述:
我是 Android 手机,用 inspector 可以查看到 webview 当中的元素,然后我找到元素去操作点击时提示:selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

我想可能是没有切换 context,但是我进去手机有 webview 的页面打印 driver.contexts 时,提示这个页面的 contexts 只有【native_app】,没有 webview 的 context

所以想问一下大佬们,用 inspector 可以查看到 webview 里面的元素,但是操作不了,并且我的页面只有 native_app,没有 webview 的 context,怎么实现操作 webview 呢?我百度搜索说如果可以直接获取到 webview 的元素,就不用切换直接当做原生来操作,但是我试了 xpath、text、name 多种定位方式都不能找到元素

inspector 截图:

共收到 17 条回复 时间 点赞
1楼 已删除
2楼 已删除

我 pagesource 也是可以打印 webview 中的元素的,但是 driver.find_element 就是一直提示找不到

有没有 appium 的日志,能不能以代码块形式贴上来?

陈恒捷 回复

这是 appium 日志的报错:

[HTTP] {"using":"xpath","value":"//android.support.v4.view.ViewPager[1]/android.widget.RelativeLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[2]/android.widget.ListView[1]/android.view.View[1]/android.widget.GridView[2]/android.view.View[1]/android.view.View[3]","sessionId":"d4433388-3306-4810-b75d-a22905514d93"}
[debug] [W3C (d4433388)] Calling AppiumDriver.findElement() with args: ["xpath","//android.support.v4.view.ViewPager[1]/android.widget.RelativeLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[2]/android.widget.ListView[1]/android.view.View[1]/android.widget.GridView[2]/android.view.View[1]/android.view.View[3]","d4433388-3306-4810-b75d-a22905514d93"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [WD Proxy] Matched '/element' to command name 'findElement'
[debug] [WD Proxy] Proxying [POST /element] to [POST http://localhost:8257/wd/hub/session/56592bc2-90df-486a-b370-785de3a90a87/element] with body: {"strategy":"xpath","selector":"//android.support.v4.view.ViewPager[1]/android.widget.RelativeLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[2]/android.widget.ListView[1]/android.view.View[1]/android.widget.GridView[2]/android.view.View[1]/android.view.View[3]","context":"","multiple":false}
[WD Proxy] Got an unexpected response: {"sessionId":"56592bc2-90df-486a-b370-785de3a90a87","status":7,"value":"An element could not be located on the page using the given search parameters."}
[debug] [MJSONWP] Matched JSONWP error code 7 to NoSuchElementError
[debug] [W3C (d4433388)] Encountered internal error running command: NoSuchElementError: An element could not be located on the page using the given search parameters.
[debug] [W3C (d4433388)]     at AndroidUiautomator2Driver.findElOrEls (/usr/local/homebrew/lib/node_modules/appium/node_modules/appium-android-driver/lib/commands/find.js:75:11)
[debug] [W3C (d4433388)]     at <anonymous>
[HTTP] <-- POST /wd/hub/session/d4433388-3306-4810-b75d-a22905514d93/element 404 635 ms - 416
[HTTP] 


这个是控制台的报错:

Traceback (most recent call last):
  File "/Users/liyaoyao/Documents/basebbs-all-autotest/iOSAutoTest/iOSAppiumTest/Android.py", line 26, in <module>
    browser.find_element_by_xpath("//android.support.v4.view.ViewPager[1]/android.widget.RelativeLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[2]/android.widget.ListView[1]/android.view.View[1]/android.widget.GridView[2]/android.view.View[1]/android.view.View[3]").click()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/appium/webdriver/webdriver.py", line 258, in find_element
    'value': value})['value']
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/appium/webdriver/errorhandler.py", line 29, in check_response
    raise wde
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/appium/webdriver/errorhandler.py", line 24, in check_response
    super(MobileErrorHandler, self).check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

有没有大佬来帮帮我

find_element 查找元素前,调用 pagesource 方法看下页面一致性,适当的使用 WebDriverWait(driver, 20).until 是可以确保页面一致性的

ltyd5788 回复

pagesource 打印是能找到元素的,我用 WebDriverWait 试了也是报错,提示找不到元素

看看非 WebView 页面使用 xpath 可以定位到元素么,还有当前页面的其他元素试试看能定位到元素么。"查找元素前,调用 pagesource 方法看下页面一致性" 这里贴出日志提示会更好

ltyd5788 回复

当前页面其他非 webview 可以用 xpath 定位,就是 webview 上的元素不能点击,坐标点击也不能点击,就是对 webview 上的元素操作不了,打印上下文 contexts 就只有 native_app,没有 webview 相关

desired capability 设置 autoWebview 属性为 true 看看

ltyd5788 回复

加了之后启动报错:selenium.common.exceptions.WebDriverException: Message: No such context found.,可以加下您 QQ 或者微信问您吗

仅楼主可见

想办法设置让 appium 采用 espresso 来识别控件,这种技术需要提供签名,找一下相关的设置

Android 的话 问开发有没有打开 Android webview debug 模式,不然好像切换不到 webview

看了下日志,没见到啥非常有效的线索,只知道在 AndroidUiautomator2Driver.findElOrEls 抛了异常。但它具体转换成了什么 uiautomator 之类的原生框架操作语句不大清楚,不好定位。再深究得周末抽时间看源码了。

用 inspector 可以查看到 webview 里面的元素,但是操作不了

这句话有点好奇,inspector 操作不了这个可以具体说下是什么情况吗?你是做了什么动作,所以产生操作不了这个结论?

陈恒捷 回复

就是用 app-inspector 看 UI 树状图,可以直接看到定位到 webview 的元素,但是我用 appium 启动找不到这个元素,提示我遇到的这个错误,xpath、text、坐标点击都用到了,就是不能在这个 webview 上用 appium 来进行操作

先试试 chrome inpect 是否能调试 Android webView,用 chrome 拾取元素标记。如不能调试多半是开发没有开启 APP webView 调试权限
webView.setWebContentsDebuggingEnabled(true);
要是没开的话,你切不到 webview 也操作不了。

请问楼主这个问题解决了吗,我也遇到了这个问题,操作不了 webview 里的元素,但是用 driver.contents 打印只有 Native,能分享下解决方案吗

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册