Skip to main content
SeleniumDecoded

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.

Common W3C: no such window Verified against Selenium 4.52.0
Exception name by language
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

// Java
org.openqa.selenium.NoSuchWindowException: no such window: target window already closed
from unknown error: web view not found
(Session info: chrome=131.0.6778.86)
# Python
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
// JavaScript (selenium-webdriver)
NoSuchWindowError: no such window: target window already closed
from unknown error: web view not found
// C#
OpenQA.Selenium.NoSuchWindowException : no such window: target window already closed
from unknown error: web view not found

Firefox reports Browsing context has been discarded. Switching to an unknown handle produces no such window without the “already closed” detail.

Common causes

  1. You closed the current tab and did not switch back. driver.close() closes the active window but leaves the driver pointed at it.
  2. A popup closed itself. OAuth, SSO and payment popups close automatically when the flow completes, often while your test is still switched into them.
  3. You switched before the new window existed. The click that opens a tab returns immediately; the handle appears a moment later.
  4. You stored a handle that is no longer valid. A handle from a previous test, or from a window the app closed and reopened.
  5. 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().

Close the child, return to the parent
Selenium 4 Stable
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.

Wait for a self-closing popup to go
Selenium 4 Stable
driver.findElement(By.id("approve")).click(); // popup closes itself
WebDriverWait 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 itself
WebDriverWait(driver, 15).until(EC.number_of_windows_to_be(1))
driver.switch_to.window(original)
await driver.findElement(By.id('approve')).click(); // popup closes itself
await driver.wait(async () => (await driver.getAllWindowHandles()).length === 1, 15000);
await driver.switchTo().window(original);
driver.FindElement(By.Id("approve")).Click(); // popup closes itself
new 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.

Switch to the new tab reliably
Selenium 4 Stable
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_handle
before = 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 finally block.
  • Never send commands to a popup after the action that closes it.
  • Wait on handle counts, never on fixed sleeps.
  • Prefer newWindow when the test controls tab creation.
  • Keep one driver per thread; another thread closing “your” window looks exactly like this error.

Go deeper