Skip to content
CodeKruPro

Switch Between Frames and Default Content

47/70

Switch Between Frames and Default Content

Key frame navigation methods:

  • driver.switchTo().frame() - switch to a frame
  • driver.switchTo().parentFrame() - go up one level
  • driver.switchTo().defaultContent() - go back to the main page

Full Page Frame Structure:

<!-- Default Content (main page) -->
<h2>Frames</h2>

<iframe id="frame1">              <!-- switchTo().frame("frame1") -->
  <h1>CodeKru</h1>
</iframe>

<iframe name="nestedFrame">       <!-- switchTo().frame("nestedFrame") -->
  <h2>Parent frame</h2>
  <iframe>                        <!-- switchTo().frame(0) from parent -->
    <h3>Child frame</h3>
  </iframe>
</iframe>

defaultContent() always returns to the main page. parentFrame() goes up one level.

Task:

  1. Navigate to https://testkru.com/Interactions/Frames
  2. Get the main page heading
  3. Switch to frame1, get its h1 text
  4. Switch back to default and get the heading again to verify

Expected output:

Main page: Frames
Frame content: CodeKru
Back to main: Frames

About this lesson

Switch back from a frame in Selenium Java with defaultContent() and parentFrame()

Learn Selenium JavaAdvancedFrames

How it works

driver.switchTo().defaultContent() returns to the top-level document, however deep you are. driver.switchTo().parentFrame() goes up exactly one level, so from the child frame on this page it returns you to nestedFrame, and a second call returns you to the main page. At the top it does nothing and throws nothing.

Switching out is part of the test, not tidying. While the driver is inside a frame, every findElement() searches only that frame, so a step that expects main-page elements fails with NoSuchElementException if an earlier step forgot to switch back.

The main page and frame1 each have a heading, which makes this page a clear way to see where the driver is: read the heading, switch, read again.

About the assignment

You read the main page's h2, step into frame1 to read its h1, and step back out to read the h2 again. The repeated read proves the driver really returned to the main page.

What you will do

  1. Before switching, read the main page's h2 and print it after the label Main page:.
  2. Switch into frame1, read its h1 and print it after the label Frame content:.
  3. Call defaultContent().
  4. Read the h2 again and print it after the label Back to main:.

Common mistakes

  • Reusing the first h2 element

    Printing a String saved from the first read proves nothing about where the driver is. Find the heading again after switching back.

  • Using parentFrame() to go to the top from deep nesting

    It climbs one level per call. From two frames down you need two calls, or one defaultContent().

  • Forgetting to switch back at all

    Inside frame1 there is no h2, so the last lookup throws NoSuchElementException.

In a real test suite

Many teams switch back in a finally block or let page objects switch in and out inside each method, so a failed step inside a frame does not leave every later step searching the wrong document.

Lesson 46 went down two levels. Lesson 48 moves from frames to browser windows, starting with the handle that identifies each one.

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: Get main page h2 heading
        // TODO: Switch to frame1, get h1 text
        // TODO: Switch to default content, get h2 heading again
        
        driver.quit();
    }
}