通过 appium.getContextHandles 获取的 webview 名称和实际要切换的 webview 名称不一致,如获取的是 “webview_com.xxxx.xxx” 类的名称,但实际要切换的是 “webview_undefinde”,或者情况相反,总之获取的名称和实际切换的 webview 不一致,报 NoSuchContextException。我现在做法是两个 webview 都尝试切换,先切换到第一个如果不行再切换到第二个,但有时成功有时失败,最蛋疼是这种现象不稳定,不好定位原因。
之前的做法:先获取后切换
for(String str:(Set) ad1.getContextHandles()){
System.out.println(str);
if(str.contains("WEBVIEW")){
ad1.context(str);
String xpath="//div[@class='list']/dl/dd";
System.out.println(ad1.findElementByXPath(xpath).getText());
ad1.context("NATIVE_APP");
}
}
现在的做法:强制切换
try{
ad1.context("WEBVIEW_com.busap.myvideo");
}catch(NoSuchContextException e){
ad1.context("WEBVIEW_undefined");
}
AppiumServer 端的日志输出:

info: --> POST /wd/hub/session/603a7ff4-aa4f-48bc-8e3c-4dd52aa5a73e/context {"name":"WEBVIEW_com.busap.myvideo"}

info: [debug] Getting a list of available webviews

info: [debug] executing cmd: D:\android-sdk\platform-tools\adb.exe -s 1a4dfcbb shell "cat /proc/net/unix"

info: [debug] WEBVIEW_20590 mapped to pid 20590

info: [debug] Getting process name for webview

info: [debug] executing cmd: D:\android-sdk\platform-tools\adb.exe -s 1a4dfcbb shell "ps"

info: [debug] Parsed pid: 20590 pkg: undefined

info: [debug] from: u0_a112,20590,538,2255732,261728,0000000000,R,com.busap.myvideo

info: [debug] returning process name: undefined

info: [debug] Available contexts:

info: [debug] ["WEBVIEW_undefined"]

info: [debug] Available contexts: NATIVE_APP,WEBVIEW_undefined

info: [debug] Responding to client with error: {"status":35,"value":{"message":"No such context

found.","origValue":"Context 'WEBVIEW_com.busap.myvideo' does not exist"},"sessionId":"603a7ff4-aa4f-48bc-8e3c-

4dd52aa5a73e"}

info: <-- POST /wd/hub/session/603a7ff4-aa4f-48bc-8e3c-4dd52aa5a73e/context 500 254.459 ms - 174

我问过 app 的开发,他说一个页面只有一个 webview,不知道 “WEBVIEW_com.busap.myvideo” 和 “WEBVIEW_undefined” 这两个东西是怎么出来的,请哪位大侠帮个忙吧😭


↙↙↙阅读原文可查看相关链接,并与作者交流