Skip to content
CodeKruPro

Switch to Frame by Name

45/70

Switch to Frame by Name

You can also switch to a frame by its name attribute.

On the Frames page, the nested frame has name="nestedFrame" and contains a parent with <h2>Parent frame</h2>.

Page DOM Structure:

<iframe name="nestedFrame" src="...">
  <!-- Inside the parent frame: -->
  <body>
    <h2>Parent frame</h2>
    <iframe src="...">
      <!-- Child frame inside -->
      <h3>Child frame</h3>
    </iframe>
  </body>
</iframe>

Task:

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

Expected output:

Parent frame text: Parent frame

About this lesson

Switch to a frame by name in Selenium Java

Learn Selenium JavaAdvancedFrames

How it works

The name attribute is the classic way to label a frame, and switchTo().frame() called with a string checks it alongside the id. The second iframe on the Frames page has name="nestedFrame" and id="frame2", so either string switches into it.

Names matter most for frames that have no id, which is common in older pages and generated widgets. When a frame has neither, you fall back to its index or to the frame element itself, both shown in the next lesson.

Inside a frame, locators work on the frame's own document only. This frame's document has an h2 and a child iframe of its own, and that child's contents are not searchable from here.

About the assignment

The frame named nestedFrame holds an h2 heading and another frame. You switch in by name, read the h2, and switch back to the main page.

What you will do

  1. Pass the frame's name to driver.switchTo().frame().
  2. Find the h2 and print its text after the label from the expected output.
  3. Return to the main page with defaultContent().

Common mistakes

  • Searching for the child's h3 from here

    The h3 lives one frame deeper. Looking for it while you are in nestedFrame throws NoSuchElementException.

  • Misspelling the name

    Names are case-sensitive. A wrong name throws NoSuchFrameException with "No frame element found by name or id".

  • Finding the h2 before switching

    The main page has its own h2, so the lookup succeeds and prints the main heading instead of the frame's.

In a real test suite

Legacy admin tools and reporting dashboards often split the screen into named frames such as a menu frame and a content frame. Tests switch by those names, which are usually more stable than positions.

Lesson 44 switched by id. Lesson 46 goes one level deeper into the child frame inside this 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: Switch to frame by name "nestedFrame"
        // TODO: Find h2 and print its text
        // TODO: Switch back to default content
        
        driver.quit();
    }
}