Skip to content
CodeKruPro

Return to the Top Document

52/80

Return to the Top Document

default_content() always returns to the main page, even from inside a frame.

Task:

  1. Print the main heading
  2. Switch into frame1 and print the h1
  3. Return to the top document and print the heading again

Print the lines below exactly.

Expected output:

Main page: Frames
Frame content: CodeKru
Back to main: Frames

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

  1. Read the level two heading before touching any frame.
  2. Switch into the first frame by the id the lesson names, then read the level one heading inside it.
  3. Reset to the top document and read the level two heading again with a fresh lookup.
  4. 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 h1 lookup 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 raises NoSuchElementException. 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 .text on it raises NoSuchElementException.

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.

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.

main.py
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()