Nested Frames

Introduction

A Nested Frame is a frame that contains one or more additional frames inside it. Unlike a normal frame, Selenium cannot directly switch to an inner frame. Instead, it must switch through each parent frame one level at a time until it reaches the required frame.

Nested frames are commonly found in enterprise applications, dashboards, document viewers, and legacy web applications where different sections of the page are divided into multiple embedded frames.

In this tutorial, you’ll learn how to automate nested frames using Selenium with Python, along with practical examples, real-world scenarios, common mistakes, and best practices.


What are Nested Frames?

A Nested Frame is a frame located inside another frame.

For example:

Main Page
│
└── frame-top
      │
      ├── frame-left
      ├── frame-middle
      └── frame-right

To access frame-middle, Selenium must first switch to frame-top, then switch to frame-middle.


Why Automate Nested Frames?

Automating nested frames helps you:

  • Test enterprise applications.

  • Verify dashboard layouts.

  • Automate embedded reports.

  • Test legacy web applications.

  • Improve automation coverage.


How to Handle Nested Frames

Nested frames require switching through each level individually.

General approach:

  1. Switch to the parent frame.

  2. Switch to the child frame.

  3. Perform the required actions.

  4. Return to the main page.


Return to the Main Page

After working inside nested frames, switch back to the main webpage.

Example:

driver.switch_to.default_content()

Example

from selenium import webdriver
from selenium.webdriver.common.by import By


# Topic: 29. Alerts and Frames - Nested Frames
# Practice site: https://the-internet.herokuapp.com/nested_frames
# Run: pytest -s 29_examples/test_04_nested_frames.py
#
# Nested frames require switching one level at a time. Here the top page has a
# frame named frame-top, and that frame contains frame-left, frame-middle, and
# frame-right.


def test_read_text_from_nested_frame():
    driver = webdriver.Chrome()

    try:
        driver.get("https://the-internet.herokuapp.com/nested_frames")

        driver.switch_to.frame("frame-top")
        driver.switch_to.frame("frame-middle")

        middle_text = driver.find_element(By.ID, "content").text
        assert middle_text == "MIDDLE"

        driver.switch_to.default_content()
    finally:
        driver.quit()

Understanding the Code

Import Required Libraries

from selenium import webdriver
from selenium.webdriver.common.by import By

These modules are required to launch the browser and locate web elements.


Create a Chrome Browser Instance

driver = webdriver.Chrome()

Starts a new Chrome browser session.


Open the Practice Website

driver.get("https://the-internet.herokuapp.com/nested_frames")

Navigates to the Nested Frames practice page.


Switch to the Parent Frame

driver.switch_to.frame("frame-top")

Switches Selenium’s focus from the main webpage to the frame-top frame.

Since frame-middle is inside frame-top, Selenium must switch here first.


Switch to the Child Frame

driver.switch_to.frame("frame-middle")

Moves Selenium from frame-top into the nested frame-middle.

Now Selenium can interact with elements inside this frame.


Locate the Element

middle_text = driver.find_element(By.ID, "content").text

Locates the element with ID content inside the nested frame and retrieves its text.


Verify the Text

assert middle_text == "MIDDLE"

Verifies that the displayed text is exactly MIDDLE.

If the expected text is not found, the test fails.


Return to the Main Page

driver.switch_to.default_content()

Returns Selenium’s focus from the nested frame back to the main webpage.


Close the Browser

driver.quit()

Closes all browser windows and ends the WebDriver session.


Practical Example

Suppose an enterprise dashboard contains multiple nested frames.

The automation script:

  • Opens the dashboard.

  • Switches to the parent frame.

  • Switches to the nested report frame.

  • Verifies the displayed report data.

  • Returns to the main page.


Automation Testing Example

Consider a banking application where the transaction summary is displayed inside nested frames.

The automation script:

  • Switches to the main frame.

  • Switches to the transaction frame.

  • Reads the account balance.

  • Verifies that the displayed balance matches the expected value.


Real-World Example

Nested frames are commonly used in:

  • Enterprise dashboards

  • Banking applications

  • Legacy web applications

  • Embedded reporting systems

  • Document management systems

  • Internal administrative portals

Examples include analytics dashboards, reporting tools, and applications with multiple embedded content sections.


Advantages of Automating Nested Frames

  • Supports complex web applications.

  • Tests multi-frame interfaces.

  • Verifies embedded content.

  • Improves automation coverage.

  • Simulates real user workflows.


Common Mistakes Beginners Make

Trying to Switch Directly to the Child Frame

You cannot switch directly to a nested frame unless Selenium is already inside its parent frame.

Always switch one level at a time.


Forgetting to Return to the Main Page

After completing operations, always use:

driver.switch_to.default_content()

Using the Wrong Frame Order

Frames must be switched in the correct hierarchy.

Inspect the HTML structure before writing your automation.


Trying to Access Nested Elements Without Switching

Attempting to locate elements inside a nested frame without switching to each parent frame usually results in a NoSuchElementException.


Best Practices

  • Understand the frame hierarchy before automation.

  • Switch one frame at a time.

  • Use meaningful frame names or stable locators.

  • Return to the main page using default_content().

  • Use Explicit Wait if nested frames load dynamically.


Conclusion

Nested frames require Selenium to switch through each parent frame before accessing child frames. By following the correct frame hierarchy and returning to the main page after completing operations, you can reliably automate applications that use multiple embedded frames. Understanding nested frame navigation is an important Selenium skill for testing enterprise and legacy web applications.


Frequently Asked Questions (FAQs)

What is a nested frame?

A nested frame is a frame located inside another frame.


Can Selenium switch directly to a child frame?

No.

Selenium must first switch to the parent frame and then to the child frame.


How do I return to the main webpage?

Use:

driver.switch_to.default_content()

Why do nested frames require multiple switch_to.frame() calls?

Because each nested frame belongs to its parent frame, Selenium must navigate through the frame hierarchy one level at a time.


Where are nested frames commonly used?

Nested frames are commonly used in enterprise dashboards, banking systems, reporting applications, document management systems, and legacy web applications.


Key Takeaways

  • Nested frames are frames contained inside other frames.

  • Selenium must switch through the parent frame before accessing a child frame.

  • Use multiple driver.switch_to.frame() calls following the frame hierarchy.

  • Return to the main page using driver.switch_to.default_content().

  • Inspect the HTML structure to understand the nesting order.

  • Proper nested frame handling is essential for automating complex web applications.