About this lesson
Selenium Python nested iframe: step into a child frame and climb out with parent_frame
Selenium Python AssignmentsAdvancedFrames
How it works
A page can hold an iframe, and that iframe can hold another one. Selenium sees one document at a time, called the current context. Every find_element call searches only there, so a heading two levels down stays hidden until you have switched through both levels.
In Python the entry point is driver.switch_to.frame. It accepts a name or id string, an integer index or a WebElement. The index counts only the frames inside the context you stand in. From the top page, index 0 is the first iframe of the whole page, not the nested one. Inside the parent, the same number means its first child. To go up exactly one level, use driver.switch_to.parent_frame(). At the top it does nothing and raises nothing.
The usual failure is a lookup in the wrong context. It surfaces as NoSuchElementException, which looks like a bad locator.
About the assignment
The Frames page has a frame that contains a second frame. You read one heading at each level and then read the outer heading again, which proves you climbed back up.
What you will do
- Switch into the outer frame using the name the lesson gives it, and read its level two heading.
- Switch again, this time by position, into the frame inside the outer one, and read its level three heading.
- Move up a single level and read the outer heading a second time.
- Print the three labelled lines in order, using the
.textattribute of each element.
Common mistakes
Jumping to the child from the top page
An index switch from the top document lands in the first iframe of the page, a different document from the one you wanted. Enter the parent first, then the child.
Reading the child heading after climbing
Back in the parent, the level three heading is out of reach.
find_elementraisesNoSuchElementExceptionwith "Unable to locate element". The locator is fine; the context is wrong.Using a frame that does not exist
The parent holds exactly one frame. Index 5 raises
NoSuchFrameException, and so does an unknown name, with the name you typed as the only clue.
In a real test suite
In a pytest suite, frame work usually hides inside a page object. A method enters the frame, returns the value, and leaves in a finally block, so tests never track the level.
The next lesson covers the other way out: jumping straight to the top document, however deep you went.
Related lessons
- Enter a Frame by ID with switch_to.frameEnter a single frame by its id, the first step of every nested case.
- Enter a Frame by Name with switch_to.frameEnter a frame by its name instead of its id.
- Return to the Top DocumentLeave all frames at once with
default_contentinstead of climbing one level.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Interactions/Frames")
# Read the parent, the child, then the parent again
finally:
driver.quit()