Skip to content
CodeKruPro

Handle Nested Frames

46/70

Handle Nested Frames

Frames can be nested inside other frames. To reach a child frame, you must first switch to the parent frame, then to the child.

On the Frames page, the nested frame (name="nestedFrame") contains a child iframe with <h3>Child frame</h3>.

Nested Frame DOM Structure:

<!-- Main page -->
<iframe name="nestedFrame">        <!-- Step 1: switch here first -->
  <h2>Parent frame</h2>
  <iframe>                          <!-- Step 2: then switch here (index 0) -->
    <h3>Child frame</h3>
  </iframe>
</iframe>

You must switch to each frame level sequentially — you cannot skip directly to the child.

Task:

  1. Navigate to https://testkru.com/Interactions/Frames
  2. Switch to the parent frame (nestedFrame)
  3. Print the h2 text (parent frame content)
  4. Switch to the child frame inside it (index 0)
  5. Print the h3 text (child frame content)

Expected output:

Parent: Parent frame
Child: Child frame

About this lesson

Handle nested iframes in Selenium Java: switch from parent frame to child frame

Learn Selenium JavaAdvancedFrames

How it works

A frame can contain frames of its own. WebDriver only switches one level at a time, and each switch is relative to the document you are in. To reach a child you switch into the parent first, then into the child.

The child frame inside nestedFrame has no id and no name, so the string form of frame() cannot reach it. Pass frame() an int instead, its index among the frames of the current document, or find the iframe element and pass that WebElement. Inside nestedFrame there is only one child, so its index is 0.

Indexes are counted per document. At the top level of the Frames page, index 0 is the first frame1, index 1 is nestedFrame and index 2 is the second frame1. Calling frame(0) twice from the top therefore enters frame1, which has no child, and the second call throws NoSuchFrameException: no such frame.

About the assignment

The parent frame holds an h2 and the child frame holds an h3. You switch into the parent, read its heading, switch from there into the child, and read the child's heading.

What you will do

  1. Switch into the parent frame by its name.
  2. Print the h2 text after the label Parent:.
  3. Switch into the child with its index inside the parent.
  4. Print the h3 text after the label Child:.

Common mistakes

  • Switching to the child from the main page

    The child is not one of the main page's frames. Index 0 from the top enters a different frame, and the lookup for the h3 then fails.

  • Calling defaultContent() between the two switches

    That returns you to the top, and the child index is now counted among the wrong frames. Stay in the parent until you have entered the child.

  • Relying on top-level indexes

    Adding one frame above another changes every index after it. Prefer names or element references when a frame has them.

In a real test suite

Nesting happens when a third-party widget is embedded inside an app that is itself framed, for example a payment iframe inside a checkout iframe. Page objects usually wrap the chain of switches in one method so tests do not repeat it.

Lesson 45 entered the parent by name. Lesson 47 shows how to move back up: one level with parentFrame() or all the way with defaultContent().

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 nestedFrame
        // TODO: Print h2 text (parent content)
        // TODO: Switch to child frame (index 0)
        // TODO: Print h3 text (child content)
        
        driver.quit();
    }
}