0
0
Selenium Pythontesting~5 mins

Nested iFrames in Selenium Python

Choose your learning style9 modes available
Introduction

Nested iFrames let you test web pages inside other web pages. They help check content that is inside multiple layers of frames.

When a web page has a frame inside another frame and you want to test elements inside the inner frame.
When you need to verify content or buttons that are not directly on the main page but inside nested frames.
When automating login forms or widgets that load inside nested iFrames.
When testing ads or third-party content embedded inside nested frames.
When you want to switch between different frames to interact with elements in each.
Syntax
Selenium Python
driver.switch_to.frame('outer_frame_name_or_id')
driver.switch_to.frame('inner_frame_name_or_id')
# Now inside inner frame
# Perform actions here
driver.switch_to.default_content()  # To go back to main page

You must switch step-by-step from the main page to outer frame, then to inner frame.

Use driver.switch_to.default_content() to return to the main page before switching to another frame.

Examples
This switches first to 'frame1', then inside it to 'frame2', then clicks a button inside the inner frame.
Selenium Python
driver.switch_to.frame('frame1')
driver.switch_to.frame('frame2')
element = driver.find_element(By.ID, 'button')
element.click()
Switching frames using iframe elements found by CSS selectors instead of names or IDs.
Selenium Python
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'iframe.outer'))
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'iframe.inner'))
Sample Program

This script opens a page with nested iframes, switches step-by-step to the inner iframe, reads a heading text, prints it, then closes the browser.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# Setup driver (make sure chromedriver is in PATH)
driver = webdriver.Chrome()

# Open a test page with nested iframes
driver.get('https://www.w3schools.com/html/tryit.asp?filename=tryhtml_iframe_nested')

# Switch to outer iframe by id
driver.switch_to.frame('iframeResult')

# Switch to inner iframe by tag name (only one inside outer)
inner_iframe = driver.find_element(By.TAG_NAME, 'iframe')
driver.switch_to.frame(inner_iframe)

# Find heading inside inner iframe
heading = driver.find_element(By.TAG_NAME, 'h1')
print(heading.text)  # Should print 'This page is displayed in an iframe'

# Go back to main content
driver.switch_to.default_content()

# Close driver
driver.quit()
OutputSuccess
Important Notes

Always switch frames in order: main page -> outer frame -> inner frame.

Use driver.switch_to.default_content() to reset to the main page before switching to another frame.

Locators like name, id, or WebElement can be used to switch frames.

Summary

Nested iFrames require switching frames step-by-step to reach inner content.

Use driver.switch_to.frame() to enter frames and driver.switch_to.default_content() to return to main page.

Testing inside nested frames helps verify content hidden inside multiple layers.