Skip to content
Selenium WebDriver · error message

Selenium: NoSuchWindowException: target window already closed

Permanent - do not retryWindows, alerts & sessions

Last verified

The exact message
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed from unknown error: web view not found

What it means

The driver is pointed at a window or tab that no longer exists, or you tried to switch to a handle that is not valid. WebDriver tracks a current window; when that window closes, the driver does not automatically move to another one. Every following command fails with this exception until you switch to a window that is still open.

The typical sequence: a link opens a new tab, the script switches to it, does its work, calls driver.close(), and then keeps sending commands without switching back. Popups that close themselves, such as OAuth or payment windows, cause the same thing without any close() in your code. A user closing the window manually during a run does it too.

What is not wrong: the session. Other windows are still controllable; only the current target is gone. If the last window closed, the session ends and you see InvalidSessionIdException instead.

What it looks like

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

main = driver.current_window_handle
driver.find_element("link text", "Open report").click()
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
new = next(h for h in driver.window_handles if h != main)
driver.switch_to.window(new)
# ... work in the new tab ...
driver.close()
driver.switch_to.window(main)   # always switch back

Why it happens

  • driver.close() was called on the current tab and the script did not switch to another handle.
  • A popup (OAuth, payment, print preview) closed itself while the driver was focused on it.
  • switch_to.window() was given a stale or wrong handle.
  • The window was closed manually or crashed during the run.

How to fix it

  1. Store the original handle before opening new windows: main = driver.current_window_handle.
  2. After closing a tab, switch back explicitly: driver.switch_to.window(main).
  3. Wait for a new window before switching: WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2)).
  4. For self-closing popups, wait until the handle disappears from driver.window_handles, then switch back.
  5. Use driver.switch_to.new_window('tab') when you need a fresh tab under your control.

How to stop it recurring

Wrap window work in a helper that records the original handle and always switches back in a finally block. Avoid relying on the order of window_handles; identify windows by excluding known handles or by checking their title or URL.

Official documentation: www.selenium.dev/documentation/webdriver/troubleshooting/errors/#nosuchwindowexception. See all Selenium error messages.

Related errors

Definitions

Quick answers

What does "NoSuchWindowException: target window already closed" mean in Selenium?

The driver is pointed at a window or tab that no longer exists, or you tried to switch to a handle that is not valid. WebDriver tracks a current window; when that window closes, the driver does not automatically move to another one. Every following command fails with this exception until you switch to a window that is still open.

How do I fix "NoSuchWindowException: target window already closed"?

1. Store the original handle before opening new windows: main = driver.current_window_handle. 2. After closing a tab, switch back explicitly: driver.switch_to.window(main). 3. Wait for a new window before switching: WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2)). 4. For self-closing popups, wait until the handle disappears from driver.window_handles, then switch back. 5. Use driver.switch_to.new_window('tab') when you need a fresh tab under your control.

Run browser automation you can watch and take over

Autonoly runs each job in a real cloud Chromium browser driven by Playwright. Timeouts and detached elements still happen; the difference is that you can watch the run live, see the step that failed, and take control of the browser to finish it.

Also see Live browser control.