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
- Call
driver.switchTo().frame()with the id from the task. - Find the
h1and print its text after the labelFrame content:. - Return to the main page with
driver.switchTo().defaultContent().
Common mistakes
Looking for the h1 without switching
The main page has an
h1of its own in the site sidebar, which is off-screen in the default 780x580 window, so a tag-name lookup forh1succeeds andgetText()returns an empty string. No exception points you at the missing switch.Using an element after leaving the frame
A
WebElementfound inside the frame cannot be used from the main page. CallinggetText()on it afterdefaultContent()throwsNoSuchElementExceptionwith "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.
Related lessons
- Switch to Frame by NameSwitch to a frame by its
nameattribute. - Switch Between Frames and Default ContentMove between the main page and a frame and back.
- Find Element by IDWhy duplicate ids, like the two frames here, catch out locators.
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.
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();
}
}