Selenium: NoSuchWindowException: target window already closed
Last verified
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not foundWhat 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 backWhy 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
- Store the original handle before opening new windows: main = driver.current_window_handle.
- After closing a tab, switch back explicitly: driver.switch_to.window(main).
- Wait for a new window before switching: WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2)).
- For self-closing popups, wait until the handle disappears from driver.window_handles, then switch back.
- 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
- InvalidSessionIdException: invalid session idselenium.common.exceptions.InvalidSessionIdException: Message: invalid session…
- StaleElementReferenceException: stale element referenceselenium.common.exceptions.StaleElementReferenceException: Message: stale…
- UnexpectedAlertPresentException: unexpected alert openselenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: Are…
Definitions
- SeleniumSelenium is an open-source browser automation project, best known for Selenium WebDriver, a library that contr
- ChromeDriverChromeDriver is the Chromium project's standalone server that implements the W3C WebDriver standard for Chrome
- CSS SelectorA CSS selector is a pattern used to identify and target specific HTML elements on a web page, widely used in b
- Headless ChromeHeadless Chrome is Google Chrome running without a visible window. It renders pages and runs JavaScript like n
- IframeAn iframe (inline frame) is an HTML element that embeds a separate web page within a parent page, creating an
- Web ElementA web element is any individual component rendered on a web page — buttons, links, input fields, images, text
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.