Skip to content

NoSuchWindowException in Android webview #877

Open
@itkhanz

Description

@itkhanz

I am stuck in a weird situation where a particular code snippet that used to work just fine a week ago is now failing often. Application under test opens a web view in a custom tab so i first wait until webview is present, and then switch to the web context. Afterwards I check if there are multiple windows/tabs, then I switch through them iteratively and check their URL to find out my desired window to perform action in webview.

Here is the partial code:-

//Get all available context handles via driver.getContextHandles()
//keep retrying until WEBVIEW_chrome is found
//switch to WEBVIEW_chrome via driver.context("WEBVIEW_chrome")
//Logic to find and switch to desired window with matching URL/Title
Set<String> windowHandles = DriverManager.getDriver().getWindowHandles();
log.debug("Found following Window Handles: {}", windowHandles);

String originalHandle = DriverManager.getDriver().getWindowHandle();  //error happens at this line
String originalUrl = DriverManager.getDriver().getCurrentUrl();
log.debug("Current Window/Tab Handle: {}, URL: {}", originalHandle, originalUrl);

Log Output:-

2025-03-31 19:32:47.882 [DEBUG] ContextActions.getContextHandles:286 - Available contexts:-
2025-03-31 19:32:48.025 [DEBUG] ContextActions.forEach:1596 - NATIVE_APP
2025-03-31 19:32:48.025 [DEBUG] ContextActions.forEach:1596 - WEBVIEW_chrome
2025-03-31 19:32:48.025 [DEBUG] ContextActions.getContextHandles:286 - Available contexts:-
2025-03-31 19:32:48.171 [DEBUG] ContextActions.forEach:1596 - NATIVE_APP
2025-03-31 19:32:48.171 [DEBUG] ContextActions.forEach:1596 - WEBVIEW_chrome
2025-03-31 19:32:48.172 [INFO] ContextActions.switchContext:308 - Switching to context: WEBVIEW_chrome
2025-03-31 19:32:48.458 [DEBUG] ContextActions.switchWindowTab:182 - Found following Window Handles: [02E5EE6E3292B2D0FB55B66542AC3B07]

The problem is that my code successfully switches to webview, and I can fetch the list of window handles but the subsequent command to get current window handle fails with the error message:-

org.openqa.selenium.NoSuchWindowException: no such window: target window already closed
from unknown error: web view not found
  (Session info: chrome=133.0.6943.137)
Build info: version: '4.26.0', revision: '8ccf0219d7'
System info: os.name: 'Mac OS X', os.arch: 'aarch64', os.version: '15.3.2', java.version: '21.0.2'
Driver info: io.appium.java_client.android.AndroidDriver
Command: [a04e9d0d6f1d4ae14a6f4a9630209761c42aed8b, getCurrentWindowHandle {}]

Appium Logs:-

[93a29039][HTTP] --> GET /session/93a29039-7eed-4ab1-a626-884cffe2e31f/contexts {}
[93a29039][AndroidUiautomator2Driver@49a9] Calling AppiumDriver.getContexts() with args: ["93a29039-7eed-4ab1-a626-884cffe2e31f"]
[93a29039][AndroidUiautomator2Driver@49a9] Getting a list of available webviews
[93a29039][ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s RF8N90E8ZBR shell cat /proc/net/unix'
[93a29039][AndroidUiautomator2Driver@49a9] Parsed 1 active devtools socket: ["@chrome_devtools_remote"]
[93a29039][AndroidUiautomator2Driver@49a9] Collecting CDP data of 1 webview
[93a29039][AndroidUiautomator2Driver@49a9] Forwarding remote port chrome_devtools_remote to a local port in range 10900..11000
[93a29039][AndroidUiautomator2Driver@49a9] You could use the 'webviewDevtoolsPort' capability to customize the starting port number
[93a29039][ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s RF8N90E8ZBR forward tcp:10900 localabstract:chrome_devtools_remote'
[93a29039][ADB] Removing forwarded port socket connection: 10900 
[93a29039][ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s RF8N90E8ZBR forward --remove tcp:10900'
[93a29039][AndroidUiautomator2Driver@49a9] CDP data collection completed
[93a29039][AndroidUiautomator2Driver@49a9] Found 1 webview: ["WEBVIEW_chrome"]
[93a29039][AndroidUiautomator2Driver@49a9] Available contexts: ["NATIVE_APP","WEBVIEW_chrome"]
[93a29039][AndroidUiautomator2Driver@49a9] Responding to client with driver.getContexts() result: ["NATIVE_APP","WEBVIEW_chrome"]
[93a29039][HTTP] <-- GET /session/93a29039-7eed-4ab1-a626-884cffe2e31f/contexts 200 163 ms - 41 
[93a29039][HTTP] --> POST /session/93a29039-7eed-4ab1-a626-884cffe2e31f/context {"name":"WEBVIEW_chrome"}
[93a29039][AndroidUiautomator2Driver@49a9] Calling AppiumDriver.setContext() with args: ["WEBVIEW_chrome","93a29039-7eed-4ab1-a626-884cffe2e31f"]
[93a29039][AndroidUiautomator2Driver@49a9] Getting a list of available webviews
[93a29039][ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s RF8N90E8ZBR shell cat /proc/net/unix'
[93a29039][AndroidUiautomator2Driver@49a9] Parsed 1 active devtools socket: ["@chrome_devtools_remote"]
[93a29039][AndroidUiautomator2Driver@49a9] Collecting CDP data of 1 webview
[93a29039][AndroidUiautomator2Driver@49a9] Forwarding remote port chrome_devtools_remote to a local port in range 10900..11000
[93a29039][AndroidUiautomator2Driver@49a9] You could use the 'webviewDevtoolsPort' capability to customize the starting port number
[93a29039][ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s RF8N90E8ZBR forward tcp:10900 localabstract:chrome_devtools_remote'
[93a29039][ADB] Removing forwarded port socket connection: 10900 
[93a29039][ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s RF8N90E8ZBR forward --remove tcp:10900'
[93a29039][AndroidUiautomator2Driver@49a9] CDP data collection completed
[93a29039][AndroidUiautomator2Driver@49a9] Found 1 webview: ["WEBVIEW_chrome"]
[93a29039][AndroidUiautomator2Driver@49a9] Available contexts: ["NATIVE_APP","WEBVIEW_chrome"]
[93a29039][AndroidUiautomator2Driver@49a9] Connecting to chrome-backed webview context 'WEBVIEW_chrome'
[93a29039][AndroidUiautomator2Driver@49a9] Found existing Chromedriver for context 'WEBVIEW_chrome'. Using it.
[93a29039][Chromedriver@be47] Matched '/url' to command name 'getUrl'
[93a29039][Chromedriver@be47] Proxying [GET /url] to [GET http://127.0.0.1:64555/session/99752998c7ec65a6b219b75c42b8f1bc/url] with no body
[93a29039][Chromedriver@be47] Got response with status 200: {"value":null}
[93a29039][AndroidUiautomator2Driver@49a9] Responding to client with driver.setContext() result: null
[93a29039][HTTP] <-- POST /session/93a29039-7eed-4ab1-a626-884cffe2e31f/context 200 165 ms - 14 
[93a29039][HTTP] --> GET /session/93a29039-7eed-4ab1-a626-884cffe2e31f/window/handles {}
[93a29039][AndroidUiautomator2Driver@49a9] Driver proxy active, passing request on via HTTP proxy
[93a29039][Chromedriver@be47] Matched '/session/93a29039-7eed-4ab1-a626-884cffe2e31f/window/handles' to command name 'getWindowHandles'
[93a29039][Chromedriver@be47] Did not know how to rewrite the original URL '/session/93a29039-7eed-4ab1-a626-884cffe2e31f/window/handles' for W3C protocol
[93a29039][Chromedriver@be47] Proxying [GET /session/93a29039-7eed-4ab1-a626-884cffe2e31f/window/handles] to [GET http://127.0.0.1:64555/session/99752998c7ec65a6b219b75c42b8f1bc/window/handles] with no body
[93a29039][Chromedriver@be47] Got response with status 200: {"value":["6911271F0AFAD9AFFEFADA840F2AB619"]}
[93a29039][HTTP] <-- GET /session/93a29039-7eed-4ab1-a626-884cffe2e31f/window/handles 200 24 ms - 46 
[93a29039][HTTP] --> GET /session/93a29039-7eed-4ab1-a626-884cffe2e31f/window/handles {}
[93a29039][AndroidUiautomator2Driver@49a9] Driver proxy active, passing request on via HTTP proxy
[93a29039][Chromedriver@be47] Matched '/session/93a29039-7eed-4ab1-a626-884cffe2e31f/window/handles' to command name 'getWindowHandles'
[93a29039][Chromedriver@be47] Did not know how to rewrite the original URL '/session/93a29039-7eed-4ab1-a626-884cffe2e31f/window/handles' for W3C protocol
[93a29039][Chromedriver@be47] Proxying [GET /session/93a29039-7eed-4ab1-a626-884cffe2e31f/window/handles] to [GET http://127.0.0.1:64555/session/99752998c7ec65a6b219b75c42b8f1bc/window/handles] with no body
[93a29039][Chromedriver@be47] Got response with status 200: {"value":["6911271F0AFAD9AFFEFADA840F2AB619"]}
[93a29039][HTTP] <-- GET /session/93a29039-7eed-4ab1-a626-884cffe2e31f/window/handles 200 3 ms - 46 
[93a29039][HTTP] --> GET /session/93a29039-7eed-4ab1-a626-884cffe2e31f/window {}
[93a29039][AndroidUiautomator2Driver@49a9] Driver proxy active, passing request on via HTTP proxy
[93a29039][Chromedriver@be47] Matched '/session/93a29039-7eed-4ab1-a626-884cffe2e31f/window' to command name 'getWindowHandle'
[93a29039][Chromedriver@be47] Did not know how to rewrite the original URL '/session/93a29039-7eed-4ab1-a626-884cffe2e31f/window' for W3C protocol
[93a29039][Chromedriver@be47] Proxying [GET /session/93a29039-7eed-4ab1-a626-884cffe2e31f/window] to [GET http://127.0.0.1:64555/session/99752998c7ec65a6b219b75c42b8f1bc/window] with no body
[93a29039][Chromedriver@be47] Got response with status 404: {"value":{"error":"no such window","message":"no such window: target window already closed\nfrom unknown error: web view not found\n  (Session info: chrome=134.0.6998.135)","stacktrace":"0   chromedriver-mac-arm64_v134.0.6998. 0x00000001046d76c8 cxxbridge1$str$ptr + 2791212\n1   chromedriver-mac-arm64_v134.0.6998. 0x00000001046cfc9c cxxbridge1$str$ptr + 2759936\n2   chromedriver-mac-arm64_v134.0.6998. 0x0000000104221e30 cxxbridge1$string$len + 92928\n3   chromedriver-mac-arm64_v134.0.6998. 0x00000001041fba00 chromedriver-mac-arm64_v134.0.6998. + 129536\n4   chromedriver-mac-arm64_v134.0.6998. 0x0000000104291418 cxxbridge1$string$len + 549096\n5   chromedriver-mac-arm64_v134.0.6998. 0x0000000104298b94 cxxbridge1$string$len + 579684\n6   chromedriver-mac-arm64_v134.0.6998. 0x000000010425d2fc cxxbridge1$string$len + 335820\n7   chromedriver-mac-arm64_v134.0.6998. 0x000000010469c6c4 cxxbridge1$str$ptr + 2549544\n8   chromedriver-mac-arm64_v134.0.6998. 0x000000010469f988 cxxbridge1$str$ptr + 2562540\n9   chrome...
[93a29039][W3C] Matched W3C error code 'no such window' to NoSuchWindowError
[93a29039][HTTP] <-- GET /session/93a29039-7eed-4ab1-a626-884cffe2e31f/window 404 2 ms - 1706 

I tried adding implicit waits but it also did not work. i also tried explicit wait to wait until no of windows to be 1 but it also did not work. On further investigating i found out that no matter which command I try for web view is not working because of this error like getting URL etc. also throws this error.

I tested on multiple real devices (Samsung Android 13, Google Pixel Android 15). Issue happens both locally as well as on cloud provider side.

Intrestingly i am not able to reproduce this error with Appium Inspector, and very very rarely it works with automated code (i dont know whats happening, probably something changed on website which closes the window automatically as i am not calling driver quite myself). Whats also incomprehendable for me is that the actual web page stays opened in foreground and its just that Appium is not able to find and interact with it in web view.

I tried to search for the issue online and went through all the pieces of advice but could not find a solution that worked for my case, so i need your kind assistance to deal through this blocker. Any suggestions to get around would be really helpful. Thank you.

Appium version 2.13.1
UiAutomator2 version 3.9.5 (also tested with 4.1.5)
Appium Java client 9.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions