driver.getContexts 中缺少 WEBVIEW 上下文

问题描述

我有一个关于 webdriver.io 的问题,我正在使用 React Native 应用程序 .apk, 网络驱动程序/5.23.0 appium_version - 1.17.0

应用中间有一个 webview 来获取用户的信息。

问题 我没有将 WEBVIEW 作为可用上下文之一,因此我无法导航到该 Web 视图,也无法导航该 Web 视图中的元素

当我在真实设备上的浏览器堆栈应用程序中的 samsung 21 上手动尝试 apk 时,我能够在检查中看到 web 视图,并且能够在检查中查看元素。 但是当我使用 webdriver 代码导航同一个 apk 时,它没有找到 webview 上下文。

任何帮助将不胜感激,如果您想查看,我可以放大并向您展示问题。

code 
contexts = await browser.getContexts();
console.log('************ STEP 2 ************',JSON.stringify(contexts));
await browser.switchContext(contexts[0]);
const continue = await $("//*[@text='Continue']");
await continue.waitFordisplayed({
  timeout: 10000
});
await continue.click()

网络驱动程序日志

2021-05-16T12:46:04.785Z INFO webdriver: [POST] https://hub-cloud.browserstack.com:443/wd/hub/session/b3cf72fbf8fdfb1db61055a650d0fbe627fa19b3/element/00810e16-1198-49b2-8c4c-5b4bcee09864/click
[0-0] 2021-05-16T12:46:09.905Z INFO webdriver: COMMAND getContexts()
[0-0] 2021-05-16T12:46:09.906Z INFO webdriver: [GET] https://hub-cloud.browserstack.com:443/wd/hub/session/b3cf72fbf8fdfb1db61055a650d0fbe627fa19b3/contexts
[0-0] 2021-05-16T12:46:10.014Z INFO webdriver: RESULT [ 'NATIVE_APP' ]
[0-0] ************ STEP 2 ************ ["NATIVE_APP"]
2021-05-16T12:46:10.017Z INFO webdriver: COMMAND switchContext("NATIVE_APP")
2021-05-16T12:46:10.018Z INFO webdriver: [POST] https://hub-cloud.browserstack.com:443/wd/hub/session/b3cf72fbf8fdfb1db61055a650d0fbe627fa19b3/context
2021-05-16T12:46:10.019Z INFO webdriver: DATA { name: 'NATIVE_APP' }

appium 日志

2021-05-16 12:46:08:332 - [HTTP] --> GET /wd/hub/session/91dd11eb-0bb0-4197-b404-9c1174bca8aa/contexts
2021-05-16 12:46:08:332 - [HTTP] {}
2021-05-16 12:46:08:334 - [debug] [MJSONWP (91dd11eb)] Calling AppiumDriver.getContexts() with args: ["91dd11eb-0bb0-4197-b404-9c1174bca8aa"]
2021-05-16 12:46:08:334 - [debug] [AndroidDriver] Getting a list of available webviews
2021-05-16 12:46:08:335 - [debug] [ADB] Running '/usr/local/.browserstack/android-sdk/platform-tools/adb -P 5037 -s RFCR204K71P shell cat /proc/net/unix'
2021-05-16 12:46:08:371 - [AndroidDriver] Retrieved potential webviews; will filter out ones with no active pages
2021-05-16 12:46:08:372 - [debug] [AndroidDriver] Found webviews: []
2021-05-16 12:46:08:373 - [debug] [AndroidDriver] Available contexts: ["NATIVE_APP"]
2021-05-16 12:46:08:373 - [debug] [MJSONWP (91dd11eb)] Responding to client with driver.getContexts() result: ["NATIVE_APP"]
2021-05-16 12:46:08:374 - [HTTP] <-- GET /wd/hub/session/91dd11eb-0bb0-4197-b404-9c1174bca8aa/contexts 200 42 ms - 86
2021-05-16 12:46:08:374 - [HTTP] 
2021-05-16 12:46:08:515 - [HTTP] --> POST /wd/hub/session/91dd11eb-0bb0-4197-b404-9c1174bca8aa/context
2021-05-16 12:46:08:515 - [HTTP] {"name":"NATIVE_APP"}
2021-05-16 12:46:08:516 - [debug] [MJSONWP (91dd11eb)] Calling AppiumDriver.setContext() with args: ["NATIVE_APP","91dd11eb-0bb0-4197-b404-9c1174bca8aa"]
2021-05-16 12:46:08:516 - [debug] [MJSONWP (91dd11eb)] Responding to client with driver.setContext() result: null
2021-05-16 12:46:08:517 - [HTTP] <-- POST /wd/hub/session/91dd11eb-0bb0-4197-b404-9c1174bca8aa/context 200 2 ms - 76
2021-05-16 12:46:08:517 - [HTTP] 
2021-05-16 12:46:08:568 - [HTTP] --> POST /wd/hub/session/91dd11eb-0bb0-4197-b404-9c1174bca8aa/element
2021-05-16 12:46:08:568 - [HTTP] {"using":"xpath","value":"//*[@text='Continue']"}
2021-05-16 12:46:08:570 - [debug] [MJSONWP (91dd11eb)] Calling AppiumDriver.findElement() with args: ["xpath","//*[@text='Continue']","91dd11eb-0bb0-4197-b404-9c1174bca8aa"]
2021-05-16 12:46:08:570 - [debug] [BaseDriver] Valid locator strategies for this request: xpath,id,class name,accessibility id,-android uiautomator
2021-05-16 12:46:08:570 - [debug] [BaseDriver] Waiting up to 0 ms for condition
2021-05-16 12:46:08:570 - [debug] [WD Proxy] Matched '/element' to command name 'findElement'
2021-05-16 12:46:08:571 - [debug] [WD Proxy] Proxying [POST /element] to [POST http://127.0.0.1:8206/wd/hub/session/9b39fbd2-a3fc-4917-8f43-d7cf35514abc/element] with body: {"strategy":"xpath","selector":"//*[@text='Continue']","context":"","multiple":false}
2021-05-16 12:46:08:613 - [WD Proxy] Got response with status 404: {"sessionId":"9b39fbd2-a3fc-4917-8f43-d7cf35514abc","value":{"error":"no such element","message":"An element Could not be located on the page using the given search parameters","stacktrace":"io.appium.uiautomator2.common.exceptions.ElementNotFoundException: An element Could not be located on the page using the given search parameters\n\tat io.appium.uiautomator2.handler.FindElement.findElement(FindElement.java:102)\n\tat io.appium.uiautomator2.handler.FindElement.safeHandle(FindElement.java:72)\n\tat io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:38)\n\tat io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:252)\n\tat io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:242)\n\tat io.appium.uiautomator2.http.ServerHandler.channelRead(ServerHandler.java:51)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366)\n\tat io.netty.channel.AbstractChannelHandlerCon...
2021-05-16 12:46:08:613 - [debug] [W3C] Matched W3C error code 'no such element' to NoSuchElementError
2021-05-16 12:46:08:614 - [debug] [MJSONWP (91dd11eb)] Encountered internal error running command: NoSuchElementError: An element Could not be located on the page using the given search parameters.
2021-05-16 12:46:08:614 - [debug] [MJSONWP (91dd11eb)]     at AndroidUiautomator2Driver.findElOrEls (/nix/store/afabwd5rpmw8z0c4a61ssiq2g0622b6q-appium-1.17.0/node_modules/appium-uiautomator2-driver/node_modules/appium-android-driver/lib/commands/find.js:75:11)
2021-05-16 12:46:08:615 - [HTTP] <-- POST /wd/hub/session/91dd11eb-0bb0-4197-b404-9c1174bca8aa/element 500 46 ms - 164
2021-05-16 12:46:08:615 - [HTTP] 

我在网上能找到的最接近的问题如下 https://github.com/appium/appium/issues/6973

解决方法

作为调试措施,您可以运行测试appium版本,如下所述 caps.setCapability("browserstack.appium_version","1.20.2"); 如果在更新观察相同的行为后写信给 support@browserstack.com