NoSuchFrameException
NoSuchFrameException: Causes and Fixes
Fix Selenium NoSuchFrameException (NoSuchFrameError in JS): iframes that load late, wrong frame names or indexes, nested frames and re-rendered iframes.
| Java | org.openqa.selenium.NoSuchFrameException |
|---|---|
| Python | selenium.common.exceptions.NoSuchFrameException |
| JavaScript | NoSuchFrameError |
| C# | OpenQA.Selenium.NoSuchFrameException |
What the error means
You asked the driver to switch into a frame, and it could not find one matching your reference, or the frame you were working in has gone away.
Frames are separate documents. The driver works in exactly one browsing context at a time, and switchTo().frame(...) only looks for child frames of the current context. That rule explains most failures: the frame is not loaded yet, your reference is wrong, or you are looking from the wrong level of nesting.
What it looks like
// Java: switching by name or idorg.openqa.selenium.NoSuchFrameException: No frame element found by name or id payment at org.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.frame(RemoteWebDriver.java:...)# Python: the message is just the reference you passedselenium.common.exceptions.NoSuchFrameException: Message: payment// JavaScript (selenium-webdriver): switching by indexNoSuchFrameError: no such frame (Session info: chrome=131.0.6778.86)// C#OpenQA.Selenium.NoSuchFrameException : No frame element found with name or id paymentWhen switching by name or id, Java, Python and C# look for an element with that id or name on the client side and throw this exception if neither exists. When switching by index or element, the browser driver decides.
Common causes
- The iframe has not loaded yet. Payment fields, embedded editors, chat widgets and ads are typically injected by JavaScript after the page loads.
- The name, id or index is wrong. Many third-party iframes have generated names (
__privateStripeFrame5371), and indexes change whenever another frame is added. - You are at the wrong nesting level. The frame is inside another frame, or you are still inside a sibling frame from a previous step.
- The frame was replaced. A re-render or reload removed the iframe element and inserted a new one, so the element reference you switched with is gone.
- The frame navigated or was removed while you were inside it. Subsequent commands fail because the current browsing context no longer exists.
How to fix it
The iframe has not loaded yet
Wait for the frame to be available and switch in one step. Locate it with a stable CSS selector rather than a generated name.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt( By.cssSelector("iframe[title='Secure card payment input frame']")));driver.findElement(By.name("cardnumber")).sendKeys("4242424242424242");driver.switchTo().defaultContent();wait = WebDriverWait(driver, 15)wait.until(EC.frame_to_be_available_and_switch_to_it( (By.CSS_SELECTOR, "iframe[title='Secure card payment input frame']")))driver.find_element(By.NAME, "cardnumber").send_keys("4242424242424242")driver.switch_to.default_content()await driver.wait(until.ableToSwitchToFrame(By.css("iframe[title='Secure card payment input frame']")), 15000);await driver.findElement(By.name('cardnumber')).sendKeys('4242424242424242');await driver.switchTo().defaultContent();var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));wait.IgnoreExceptionTypes(typeof(NoSuchFrameException));wait.Until(d =>{ var frame = d.FindElement(By.CssSelector("iframe[title='Secure card payment input frame']")); d.SwitchTo().Frame(frame); return true;});driver.FindElement(By.Name("cardnumber")).SendKeys("4242424242424242");driver.SwitchTo().DefaultContent();The frame being available does not mean its content has rendered. After switching, still wait for the element you need inside the frame.
The name, id or index is wrong
Inspect the iframe element in DevTools. If its name or id looks generated, switch using a WebElement found by a stable attribute (title, src fragment, a data- attribute on the iframe or its container). Avoid indexes except for pages you fully control.
WebElement editorFrame = driver.findElement(By.cssSelector("iframe[src*='/editor/']"));driver.switchTo().frame(editorFrame);editor_frame = driver.find_element(By.CSS_SELECTOR, "iframe[src*='/editor/']")driver.switch_to.frame(editor_frame)const editorFrame = await driver.findElement(By.css("iframe[src*='/editor/']"));await driver.switchTo().frame(editorFrame);IWebElement editorFrame = driver.FindElement(By.CssSelector("iframe[src*='/editor/']"));driver.SwitchTo().Frame(editorFrame);You are at the wrong nesting level
Always start from the top document, then step down one level at a time. parentFrame() moves up one level; defaultContent() jumps to the top.
driver.switchTo().defaultContent();driver.switchTo().frame(driver.findElement(By.id("outer")));driver.switchTo().frame(driver.findElement(By.id("inner")));// ... work inside inner ...driver.switchTo().parentFrame(); // back to outerdriver.switchTo().defaultContent(); // back to topdriver.switch_to.default_content()driver.switch_to.frame(driver.find_element(By.ID, "outer"))driver.switch_to.frame(driver.find_element(By.ID, "inner"))# ... work inside inner ...driver.switch_to.parent_frame()driver.switch_to.default_content()await driver.switchTo().defaultContent();await driver.switchTo().frame(await driver.findElement(By.id('outer')));await driver.switchTo().frame(await driver.findElement(By.id('inner')));// ... work inside inner ...await driver.switchTo().parentFrame();await driver.switchTo().defaultContent();driver.SwitchTo().DefaultContent();driver.SwitchTo().Frame(driver.FindElement(By.Id("outer")));driver.SwitchTo().Frame(driver.FindElement(By.Id("inner")));// ... work inside inner ...driver.SwitchTo().ParentFrame();driver.SwitchTo().DefaultContent();A common bug is a page object method that switches into a frame and returns without switching back. The next page object then searches for a frame from inside the previous one.
The frame was replaced
Do not hold on to the iframe WebElement across actions that can re-render the host page. Re-find it immediately before switching, ideally inside the wait shown above, which retries until a fresh frame is ready.
The frame navigated or was removed while you were inside it
If an action inside the frame causes it to close (a payment frame that disappears on success), switch to defaultContent() straight after the action and continue from the top document. Treat further errors as NoSuchWindowException territory if the whole popup or tab went away.
How to prevent it
- Encapsulate each frame in a page component that switches in, does its work, and switches back in a
finallyblock. - Always reset to
defaultContent()at the start of a frame operation instead of assuming where you are. - Locate frames by stable attributes, never by generated names or indexes.
- Wait for frames as you would for any other asynchronous element.
- See the iframes lesson and the iframe interaction recipe for complete helpers.
Related errors
- NoSuchElementException: the classic symptom of forgetting to switch into a frame at all.
- StaleElementReferenceException: an element inside a frame that reloaded.
- NoSuchWindowException: the window, not just the frame, is gone.
- NoSuchShadowRootException: shadow DOM is a different boundary that needs a different technique.
Go deeper
- iFrames and Frames Learn to switch between frames and iframes to interact with embedded content.
- Expected Conditions Use built-in expected conditions for common wait scenarios like visibility, clickability, and text presence.
- Common Errors Diagnose and fix the most common Selenium errors and exceptions.