Skip to content
CodeKruPro

Enter a Frame by ID with switch_to.frame

49/80

Enter a Frame by ID with switch_to.frame

Elements inside a frame are invisible until you switch into that frame. This page has two frames with id frame1. Switching by that id enters the first one.

Task:

  1. Open Frames
  2. Switch into frame1
  3. Print the h1 text

Print the lines below exactly.

Expected output:

Frame content: CodeKru

About this lesson

Selenium Python switch_to.frame by id: step into an iframe before reading it

Selenium Python AssignmentsAdvancedFrames

How it works

A frame is a separate document embedded in the page. The driver only looks at one document at a time, and after loading the page it looks at the top one. An element inside a frame does not exist for it. The failure is quiet: a search for the page heading finds a different element in the top document, and nothing warns you that the one you wanted is inside the frame.

driver.switch_to.frame() moves the driver into a frame. It accepts a string, and Selenium Python treats that string as an id first and then as a name. It also takes a number for position, or a frame element you have already found. This page has two frames that share the id frame1. Switching by that string enters the first match, which holds the heading the exercise wants. The second one has other text, and a list from find_elements shows both, so you can count them if you are unsure.

The call returns None, so there is nothing to store. After the switch, ordinary find_element calls look inside the frame. The top document has its own heading with the same tag, but it is hidden, so .text returns an empty string and prints a blank, so reading it by mistake prints a blank. If you give a name that does not exist, you get NoSuchFrameException.

About the assignment

Go into the first frame on the Frames page and print the heading it contains.

What you will do

  1. Load the Frames page, as the starter does.
  2. Switch into the frame whose id is frame1 with switch_to.frame.
  3. Find the h1 element and read its text.
  4. Print that text after the label. The starter closes the browser afterwards.

Common mistakes

  • Reading before switching

    The lookup does not fail. It returns the top-level heading, whose text is an empty string, so the output shows the label with nothing after it.

  • A wrong identifier

    A name that matches neither an id nor a name raises NoSuchFrameException. An index that is too large raises it too, with the message "no such frame".

  • Assuming ids are unique

    Two frames here share one id. The string picks the first, but another page may order them differently, so check with find_elements when a result looks odd.

In a real test suite

Teams prefer frame_to_be_available_and_switch_to_it inside a WebDriverWait, so the switch waits for the frame to load. Page objects for embedded widgets, like a payment form, switch in on entry and back out on exit.

The next lesson reaches a frame by its name instead of its id.

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")
    # Switch into frame1 and print the h1
finally:
    driver.quit()