About this lesson
Selenium Python default_content: get back to the main page from inside an iframe
Selenium Python AssignmentsAdvancedFrames
How it works
Switching into an iframe changes where Selenium looks, and the main page is no longer searched. driver.switch_to.default_content() resets the context to the top-level document, whatever the nesting. It takes no arguments, and calling it twice in a row is harmless.
Compare it with parent_frame(), which climbs one level. That is enough for a single frame and wrong after two. default_content() is the reliable reset, and many scripts call it before each new main-page step.
Python adds a detail. An element saved while inside a frame belongs to that frame. After you leave, using the variable does not return the old text. Here it raised NoSuchElementException with the message "element not found". Look the element up again each time you enter a frame.
The same tag can exist in several documents. The main page has a hidden h1 of its own, so asking for that tag outside the frame returns an empty string and no error. A silent blank is harder to debug than an exception.
About the assignment
Read a heading on the main page, step into a frame to read a different heading, then come back and read the main heading once more.
What you will do
- Read the level two heading before touching any frame.
- Switch into the first frame by the id the lesson names, then read the level one heading inside it.
- Reset to the top document and read the level two heading again with a fresh lookup.
- Print three lines with the labels from the task, joining each label and its text with
+.
Common mistakes
Reading the frame heading from the main page
Without the switch, the
h1lookup finds a different, hidden element. Your line shows the label and nothing after it, with no traceback.Searching for the main heading while still inside
The frame has no
h2, so the lookup raisesNoSuchElementException. It usually means you forgot to leave the frame.Reusing a saved element after the reset
A variable assigned inside the frame stops working once you leave. Calling
.texton it raisesNoSuchElementException.
In a real test suite
Teams often wrap frames in a context manager or fixture so the reset runs even when a check fails. A shared conftest.py can also reset after each test.
After frames comes window handling. Lesson 53 starts with the simplest question: how many windows are open.
Related lessons
- Enter a Frame by ID with switch_to.frameEnter a frame by id, the move you undo here.
- Read a Nested FrameNested frames, where
parent_frame()climbs one step anddefault_content()climbs all of them. - Combine a Wait, a Frame, and an Alert in One ScriptCombines a wait, a frame and an alert in one script.
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 main heading, the frame heading, then the main heading again
finally:
driver.quit()