Skip to content
QA Knowledge Hub

NoSuchWindowException / NoSuchFrameException

Updated 2026-11-13 · 2 min read

Window handles and iframe paths identify different browsing contexts. Restore a surviving tab and enter its frame hierarchy before locating child controls.

Recognise the error

The official Java API names this exception:

org.openqa.selenium.NoSuchWindowException

This is the documented exception identifier, not a reproduced stack trace. Driver-specific wording and diagnostic details vary. Keep the full message from your own run; no browser error was reproduced for this page. Java fragments below assume an existing Selenium 4 WebDriver driver and standard Selenium imports. They were checked against the APIs but not compiled or run here.

The companion exception identifier in Selenium's official Java API is:

org.openqa.selenium.NoSuchFrameException

A window error concerns a tab/window handle; a frame error concerns an iframe in the current document context. Neither is fixed by changing the child element's locator.

The WebDriver specification defines the corresponding protocol error string as:

no such window

For an unavailable frame, the protocol error string is no such frame.

1. The test closed its current tab

Save the original handle before opening the extra tab. After closing the extra tab, explicitly switch back to a surviving handle.

String original = driver.getWindowHandle();
driver.switchTo().newWindow(WindowType.TAB);
// Perform the extra-tab work here.
driver.close();
driver.switchTo().window(original);

This assumes the original tab remains open. A handle from a previous session cannot be reused. For app-opened windows, collect the handle set before the click, wait for a new handle, and select the difference; do not rely on set iteration order.

2. The iframe has not appeared, or the frame index changed

Use a stable frame locator and a wait instead of a numeric index that changes when a banner adds another iframe.

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
driver.switchTo().defaultContent();
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(
    By.cssSelector("iframe#payment")));
// Locate fields inside the payment document here.
driver.switchTo().defaultContent();

3. The frame is nested inside another frame

Select each level from its parent. Start from the top-level document when you do not know the current depth.

driver.switchTo().defaultContent();
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("checkout")));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("payment")));
driver.switchTo().defaultContent();

A navigation can remove or replace a frame. Re-enter the new hierarchy after that navigation instead of keeping an old frame reference.

Prevention and interview answer

Keep window handles local to the test, restore frame context after helper methods, and record surviving handles on failure. “I verify the tab still exists, switch to it, reset to its main document, and then enter the required iframe chain.”

If context is correct but the child is missing, see NoSuchElementException. For disposed Playwright pages, see target closed.

Quick answers

Are a window and an iframe the same context?

No. Select the correct window first, then navigate its frame hierarchy.

Does defaultContent switch to the original tab?

No. It returns to the top-level document of the current window.

Sources

Want a project you can show in interviews?

The portfolio project kit walks you through testing a deliberately buggy app with Selenium or Playwright and API tests, then explaining it. The app itself is free.