You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地iOS Appium切换WebView失败求助:无可用WebView

问题:无法导航到WebView;不存在任何WebView

期望能力(Desired capabilities)

{
  "appium:platformName": "iOS",
  "appium:platformVersion": "15.0",
  "appium:deviceName": "iPhone 13 pro Max",
  "appium:automationName": "XCUITest",
  "udid": "0B2392E0-F3A7-4860-8E01-98C2B43EE6C9",
  "autoWebview": "true"
}

环境信息

  • Appium Server GUI : 1.22.0
  • Xcode - 13.0
  • appium inspector - 2022.2.1
  • Java - 18.0.2.1
  • Selenium - 3.11.0
  • java-client - 6.0.0-BETA5
  • Eclipse版本: 2022-06 (4.24.0)
  • 操作系统名称: Mac OS X
  • 操作系统版本: 12.5.1

问题说明

该测试在BrowserStack上运行正常,但本地环境切换到WebView时失败。需要添加哪些额外期望能力才能从原生上下文切换到WebView?

Appium日志

[RemoteDebugger] Checking for bundle identifiers: com.apple.WebKit.WebContent, process-com.apple.WebKit.WebContent, process-SafariViewService, com.apple.SafariViewService, *, xxxxx
[RemoteDebugger] Found app id key 'PID:41591' for bundle 'com.apple.WebKit.WebContent'
[RemoteDebugger] Found app id key 'PID:41591' for bundle 'process-com.apple.WebKit.WebContent'
[RemoteDebugger] Found app id key 'PID:41591' for bundle 'process-SafariViewService'
[RemoteDebugger] Found app id key 'PID:41591' for bundle 'com.apple.SafariViewService'
[RemoteDebugger] Found app id key 'PID:41591' for bundle '*'
[RemoteDebugger] Found app id key 'PID:41591' for bundle 'xxxxxxx'
[RemoteDebugger] Trying out the possible app ids: PID:41591 (try #1 of 20)
[RemoteDebugger] Attempting app 'PID:41591'
[RemoteDebugger] Sending '_rpc_forwardGetListing:' message to app 'PID:41591' (id: 376): 'connectToApp'
[RemoteDebugger] Received response from send (id: 376): '["PID:41591",{}]'
[RemoteDebugger] Sending to Web Inspector took 1ms
[RemoteDebugger] Empty page dictionary received
[RemoteDebugger] Error checking application: 'Empty page dictionary received'. Retrying connection
[RemoteDebugger] Current applications available:
[RemoteDebugger]     Application: "PID:41591"
[RemoteDebugger]         id: "PID:41591"
[RemoteDebugger]         isProxy: false
[RemoteDebugger]         name: "com.apple.WebKit.WebContent"
[RemoteDebugger]         bundleId: "process-com.apple.WebKit.WebContent"
[RemoteDebugger]         hostId: undefined
[RemoteDebugger]         isActive: true
[RemoteDebugger]         isAutomationEnabled: "Unknown"
[RemoteDebugger]     Application: "PID:41592"
[RemoteDebugger]         id: "PID:41592"
[RemoteDebugger]         isProxy: false
[RemoteDebugger]         name: "com.apple.WebKit.Networking"
[RemoteDebugger]         bundleId: "process-com.apple.WebKit.Networking"
[RemoteDebugger]         hostId: undefined
[RemoteDebugger]         isActive: true
[RemoteDebugger]         isAutomationEnabled: "Unknown"
[RemoteDebugger]     Application: "PID:41589"
[RemoteDebugger]         id: "PID:41589"
[RemoteDebugger]         isProxy: false
[RemoteDebugger]         name: "xxxxxx"
[RemoteDebugger]         bundleId: "process-Zetadocs.Approval.Mobile.iOS"
[RemoteDebugger]         hostId: undefined
[RemoteDebugger]         isActive: true
[RemoteDebugger]         isAutomationEnabled: "Unknown"
[RemoteDebugger] Checking for bundle identifiers: com.apple.WebKit.WebContent, process-com.apple.WebKit.WebContent, process-SafariViewService, com.apple.SafariViewService, *, xxxxxxx
[RemoteDebugger] Found app id key 'PID:41591' for bundle 'com.apple.WebKit.WebContent'
[RemoteDebugger] Found app id key 'PID:41591' for bundle 'process-com.apple.WebKit.WebContent'
[RemoteDebugger] Found app id key 'PID:41591' for bundle 'process-SafariViewService'
[RemoteDebugger] Found app id key 'PID:41591' for bundle 'com.apple.SafariViewService'
[RemoteDebugger] Found app id key 'PID:41591' for bundle '*'
[RemoteDebugger] Found app id key 'PID:41591' for bundle 'xxxxxx'
[RemoteDebugger] Trying out the possible app ids: PID:41591 (try #1 of 20)
[RemoteDebugger] Attempting app 'PID:41591'
[RemoteDebugger] Sending '_rpc_forwardGetListing:' message to app 'PID:41591' (id: 378): 'connectToApp'
[RemoteDebugger] Received response from send (id: 378): '["PID:41591",{}]'
[RemoteDebugger] Sending to Web Inspector took 1ms
[RemoteDebugger] Empty page dictionary received
[RemoteDebugger] Error checking application: 'Empty page dictionary received'. Retrying connection
[RemoteDebugger] Could not connect to a valid app after 20 tries.
[XCUITest] No available web pages: Could not connect to a valid app after 20 tries.
[XCUITest] No web frames found.
[XCUITest] {}
[WD Proxy] Matched '/session/35db46e3-9aab-48f8-afe5-d6e11abe8ff1' to command name 'deleteSession'
[WD Proxy] Proxying [DELETE /session/35db46e3-9aab-48f8-afe5-d6e11abe8ff1] to [DELETE http://127.0.0.1:8100/session/2DAE2402-0733-43DC-A98E-1CDD488A2CA4] with no body
[WD Proxy] Got response with status 200: {"value":null,"sessionId":null}
[DevCon Factory] Releasing connections for 0B2392E0-F3A7-4860-8E01-98C2B43EE6C9 device on any port number
[DevCon Factory] Found cached connections to release: ["0B2392E0-F3A7-4860-8E01-98C2B43EE6C9:8100"]
[DevCon Factory] Cached connections count: 0
[XCUITest] Not clearing log files. Use `clearSystemFiles` capability to turn on.
[XCUITest] Found a remote debugger session. Removing...
[RemoteDebugger] Disconnecting from remote debugger
[RemoteDebugger] Cleaning up listeners
[IOSSimulatorLog] Stopping iOS log capture
[BaseDriver] Event 'newSessionStarted' logged at 1664949451836 (11:27:31 GMT+0530 (India Standard Time))
[W3C] Encountered internal error running command: Error: Could not navigate to webview; there are none!
[W3C]     at spinHandles (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/lib/commands/context.js:161:13)
[W3C]     at spinHandles (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/lib/commands/context.js:168:14)
[W3C]     at spinHandles (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/lib/commands/context.js:168:14)
[W3C]     at spinHandles (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/lib/commands/context.js:168:14)
[W3C]     at spinHandles (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/lib/commands/context.js:168:14)
[W3C]     at spinHandles (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/lib/commands/context.js:168:14)
[W3C]     at spinHandles (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/lib/commands/context.js:168:14)
[W3C]     at spinHandles (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/lib/commands/context.js:168:14)
[W3C]     at spinHandles (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/lib/commands/context.js:168:14)
[W3C]     at XCUITestDriver.navToViewWithTitle (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/lib/commands/context.js:186:3)
[W3C]     at XCUITestDriver.navToInitialWebview (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/lib/commands/context.js:222:5)
[W3C]     at XCUITestDriver.start (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/lib/driver.js:528:7)
[W3C]     at XCUITestDriver.createSession (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/lib/driver.js:262:7)
[W3C]     at AppiumDriver.createSession (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/lib/appium.js:387:35)
[HTTP] <-- POST /wd/hub/session 500 95464 ms - 677
[HTTP] 

解决方案建议

1. 调整WebView切换时机

autoWebview设为true会在会话启动时立即尝试切换,但此时WebView可能未加载完成。建议改为在测试代码中手动等待WebView出现后再切换:

// 等待WebView上下文出现
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));

// 获取所有上下文并切换到WebView
Set<String> contexts = driver.getContextHandles();
for (String context : contexts) {
    if (context.contains("WEBVIEW")) {
        driver.context(context);
        break;
    }
}

2. 添加WebView调试相关能力

在期望能力中补充以下参数:

"appium:enableWebviewDebugging": true,
"appium:autoWebviewTimeout": 60000,
"appium:webviewDevtoolsPort": 27753
  • enableWebviewDebugging:强制开启WebView调试模式
  • autoWebviewTimeout:延长等待WebView出现的超时时间至60秒
  • webviewDevtoolsPort:指定固定调试端口,避免端口冲突

3. 检查App的WebView配置

确保被测App的Info.plist中添加NSAllowsArbitraryLoads(若加载HTTP页面),且WKWebView开启调试:

webView.configuration.preferences.setValue(true, forKey: "developerExtrasEnabled")

4. 升级依赖版本

当前java-client 6.0.0-BETA5为测试版,建议升级到与Appium 1.22.0兼容的稳定版本(如7.6.0);同时将Selenium升级到3.141.59,避免版本不兼容问题。

内容的提问来源于stack exchange,提问作者Pooja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 07:35:21