首页 > 解决方案 > Appium v​​1.17.1 无法在 iOS 中切换 WebView 上下文

问题描述

以前的版本 (1.15.1) 我能够执行此代码。

Set<String> contextNames = driver.getContextHandles();
    for (String contextName : contextNames) {
        System.out.println(contextName); //prints out something like NATIVE_APP \n WEBVIEW_1
    }
driver.context((String) contextNames.toArray()[1]); // set context to WEBVIEW_1

但是在更新到最新版本(1.17.1)后,它遇到了这个错误。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at com.sapphire.appclient.automation.page.AbstractPage.getContext(AbstractPage.java:53)
...
...

标签: seleniumappium

解决方案


您使用的语法是错误的。

这是针对 Python 的:

如果你知道上下文:

driver.switch_to.context['NATIVE_APP]   #appium chrome driver
driver.switch_to.context['WEB_VIEW_chrome]    # selenium chrome driver

返回列表中所有可用的上下文:

driver.contexts

所以你可以像这样使用它:

driver.switch_to.context([0])

注意:web_view 始终是列表中的最后一个键,因此您始终可以使用以下命令将上下文更改为 webview(FireFox、Chorme、Safari):

contexts = driver.contexts
driver.switch_to.contexts[-1]

返回当前上下文:

driver.context

推荐阅读