NoSuchWindowException
NoSuchWindowException: Causes and Fixes
Fix Selenium NoSuchWindowException (NoSuchWindowError in JS): closed tabs, self-closing popups, stale window handles and switching before a new tab opens.
| Java | org.openqa.selenium.NoSuchWindowException |
|---|---|
| Python | selenium.common.exceptions.NoSuchWindowException |
| JavaScript | NoSuchWindowError |
| C# | OpenQA.Selenium.NoSuchWindowException |
What the error means
The driver is pointed at a window (a top-level browsing context: a tab or a popup) that no longer exists, or you asked it to switch to a window handle it does not recognise.
WebDriver does not automatically follow you when tabs open or close. It keeps targeting the window you last switched to. If that window closes, every subsequent command fails with “no such window” until you explicitly switch to a window that is still open.
What it looks like
// Javaorg.openqa.selenium.NoSuchWindowException: no such window: target window already closedfrom unknown error: web view not found (Session info: chrome=131.0.6778.86)# Pythonselenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closedfrom unknown error: web view not found// JavaScript (selenium-webdriver)NoSuchWindowError: no such window: target window already closedfrom unknown error: web view not found// C#OpenQA.Selenium.NoSuchWindowException : no such window: target window already closedfrom unknown error: web view not foundFirefox reports Browsing context has been discarded. Switching to an unknown handle produces no such window without the “already closed” detail.
Common causes
- You closed the current tab and did not switch back.
driver.close()closes the active window but leaves the driver pointed at it. - A popup closed itself. OAuth, SSO and payment popups close automatically when the flow completes, often while your test is still switched into them.
- You switched before the new window existed. The click that opens a tab returns immediately; the handle appears a moment later.
- You stored a handle that is no longer valid. A handle from a previous test, or from a window the app closed and reopened.
- The browser tab crashed. A renderer crash can discard the window; in that case you often get InvalidSessionIdException soon after.
How to fix it
You closed the current tab and did not switch back
Record the original handle before opening anything, and switch back to it immediately after close().
String original = driver.getWindowHandle();// ... switch to the child and work in it ...driver.close();driver.switchTo().window(original);original = driver.current_window_handle# ... switch to the child and work in it ...driver.close()driver.switch_to.window(original)const original = await driver.getWindowHandle();// ... switch to the child and work in it ...await driver.close();await driver.switchTo().window(original);string original = driver.CurrentWindowHandle;// ... switch to the child and work in it ...driver.Close();driver.SwitchTo().Window(original);A popup closed itself
After the action that completes the popup flow (clicking “Allow” or “Pay”), do not send any more commands to the popup. Wait for the window count to drop, then switch back to the original window.
driver.findElement(By.id("approve")).click(); // popup closes itselfWebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));wait.until(ExpectedConditions.numberOfWindowsToBe(1));driver.switchTo().window(original);driver.find_element(By.ID, "approve").click() # popup closes itselfWebDriverWait(driver, 15).until(EC.number_of_windows_to_be(1))driver.switch_to.window(original)await driver.findElement(By.id('approve')).click(); // popup closes itselfawait driver.wait(async () => (await driver.getAllWindowHandles()).length === 1, 15000);await driver.switchTo().window(original);driver.FindElement(By.Id("approve")).Click(); // popup closes itselfnew WebDriverWait(driver, TimeSpan.FromSeconds(15)) .Until(d => d.WindowHandles.Count == 1);driver.SwitchTo().Window(original);getAllWindowHandles does not need a valid current window, so it is safe to poll even after the popup is gone.
You switched before the new window existed
Wait for the handle count to increase, then switch to the handle that was not there before. Do not assume the last handle in the list is the newest; ordering is not guaranteed.
String original = driver.getWindowHandle();Set<String> before = driver.getWindowHandles();driver.findElement(By.linkText("Open report")).click();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));wait.until(d -> d.getWindowHandles().size() > before.size());String newHandle = driver.getWindowHandles().stream() .filter(h -> !before.contains(h)).findFirst().orElseThrow();driver.switchTo().window(newHandle);original = driver.current_window_handlebefore = set(driver.window_handles)driver.find_element(By.LINK_TEXT, "Open report").click()
WebDriverWait(driver, 10).until(lambda d: len(d.window_handles) > len(before))new_handle = next(h for h in driver.window_handles if h not in before)driver.switch_to.window(new_handle)const original = await driver.getWindowHandle();const before = await driver.getAllWindowHandles();await driver.findElement(By.linkText('Open report')).click();
await driver.wait(async () => (await driver.getAllWindowHandles()).length > before.length, 10000);const newHandle = (await driver.getAllWindowHandles()).find(h => !before.includes(h));await driver.switchTo().window(newHandle);string original = driver.CurrentWindowHandle;var before = driver.WindowHandles.ToList();driver.FindElement(By.LinkText("Open report")).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(10)) .Until(d => d.WindowHandles.Count > before.Count);string newHandle = driver.WindowHandles.First(h => !before.Contains(h));driver.SwitchTo().Window(newHandle);If your test opens the tab itself rather than clicking a link, use driver.switchTo().newWindow(WindowType.TAB) (Python switch_to.new_window('tab'), JS switchTo().newWindow('tab'), C# SwitchTo().NewWindow(WindowType.Tab)), which creates the tab and switches to it in one call. The switch to new tab recipe wraps this into a helper.
You stored a handle that is no longer valid
Window handles are only meaningful within one session and only while that window is open. Do not keep them in static fields or share them between tests. Look them up when needed.
The browser tab crashed
If the window disappeared without anyone closing it, check for renderer crashes: memory-heavy pages in containers with a small /dev/shm are the usual culprit. Increase shared memory or add --disable-dev-shm-usage.
How to prevent it
- Wrap multi-window flows in a helper that records the original handle, switches, runs the work, and always switches back in a
finallyblock. - Never send commands to a popup after the action that closes it.
- Wait on handle counts, never on fixed sleeps.
- Prefer
newWindowwhen the test controls tab creation. - Keep one driver per thread; another thread closing “your” window looks exactly like this error.
Related errors
- NoSuchFrameException: the frame-level equivalent, when the frame you target is gone.
- InvalidSessionIdException: the whole browser, not just one window, is gone.
- UnhandledAlertException: an alert is blocking commands to the window.
- NoSuchElementException: you are in a valid window, but the wrong one.
Go deeper
- Windows and Tabs Handle multiple browser windows and tabs, switch between them, and manage popup windows.
- Common Errors Diagnose and fix the most common Selenium errors and exceptions.
- Expected Conditions Use built-in expected conditions for common wait scenarios like visibility, clickability, and text presence.