Skip to content
CodeKruPro

Switch to Frame by ID

44/70

Switch to Frame by ID

Frames (iframes) are separate HTML documents embedded in a page. To interact with elements inside a frame, you must first switch to it.

driver.switchTo().frame("frameId") switches to a frame by its ID.

On the Frames page, the first frame has id="frame1" and contains an <h1> with text "CodeKru".

Page DOM Structure:

<!-- Main page content -->
<h2>Frames</h2>

<iframe id="frame1" srcdoc="...">
  <!-- Separate document inside frame1: -->
  <body>
    <h1>CodeKru</h1>
  </body>
</iframe>

You must switchTo().frame() before you can access elements inside the iframe.

Task:

  1. Navigate to https://testkru.com/Interactions/Frames
  2. Switch to the frame with ID frame1
  3. Find the h1 element and print its text
  4. Switch back to default content

Expected output:

Frame content: CodeKru

About this lesson

Switch to an iframe by id in Selenium Java with switchTo().frame()

Learn Selenium JavaAdvancedFrames

How it works

An iframe holds a separate HTML document inside the page. WebDriver searches one document at a time, so elements inside a frame are invisible to findElement() until you call driver.switchTo().frame(). Every later lookup then runs inside that frame, until you switch out.

Called with a string, frame() accepts either the frame's id or its name. On the Frames page the first iframe has id="frame1" and name="singleFrame", so both strings reach it. A string that matches neither throws NoSuchFrameException: No frame element found by name or id.

This page also has a trap: a second iframe further down uses the same id and name. frame("frame1") always enters the first match. To reach the second, find both with findElements(By.id("frame1")) and pass the one you want to frame() as a WebElement.

About the assignment

The first frame on the Frames page contains a single h1. You switch into it by id, read the heading, and switch back out so the driver is on the main page again.

What you will do

  1. Call driver.switchTo().frame() with the id from the task.
  2. Find the h1 and print its text after the label Frame content:.
  3. Return to the main page with driver.switchTo().defaultContent().

Common mistakes

  • Looking for the h1 without switching

    The main page has an h1 of its own in the site sidebar, which is off-screen in the default 780x580 window, so a tag-name lookup for h1 succeeds and getText() returns an empty string. No exception points you at the missing switch.

  • Using an element after leaving the frame

    A WebElement found inside the frame cannot be used from the main page. Calling getText() on it after defaultContent() throws NoSuchElementException with "element not found".

  • Expecting getTitle() to read the frame

    The title always belongs to the top-level page, so inside the frame getTitle() still returns the main page's title.

In a real test suite

Frames hold payment forms, rich-text editors, embedded videos and third-party widgets such as chat or captcha boxes. Tests switch in, do one job, and switch out straight away, so later steps do not search the wrong document.

Lesson 43 handled browser dialogs, another switchTo() target. Lesson 45 enters a different frame on this page by its name.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Interactions/Frames");
        
        // TODO: Switch to frame with ID "frame1"
        // TODO: Find h1 and print its text
        // TODO: Switch back to default content
        
        driver.quit();
    }
}