Skip to content
CodeKruPro

Enter a Frame by Name with switch_to.frame

50/80

Enter a Frame by Name with switch_to.frame

switch_to.frame also accepts the frame name. nestedFrame contains the parent heading.

Task:

  1. Switch into the frame named nestedFrame
  2. Print the h2 text
  3. Return to the top document

Print the lines below exactly.

Expected output:

Parent frame text: Parent frame

About this lesson

Selenium Python switch_to.frame by name: enter a named frame and come back out

Selenium Python AssignmentsAdvancedFrames

How it works

The string you give switch_to.frame does double duty. Selenium looks for a frame with that id and, if it finds none, for one with that name. So a frame with no id, or with an id you cannot rely on, can still be reached by its name attribute. On this page the frames have both attributes, and in the page source the names are different from the ids. The name nestedFrame and the id frame2 belong to the same frame.

Inside the frame, the driver sees only that frame document. This one has a parent heading in an h2 element, and no h1. A search for h1 here returns an empty list from find_elements, which is a handy way to tell where you are. The top document has an h2 of its own with the text Frames, so a read before the switch gives you the wrong words.

Leaving is as important as entering. driver.switch_to.default_content() sends the driver back to the top document, whatever depth it was at. If you skip it, the next lookup still searches inside the frame, and anything on the main page seems to have vanished. Calling it when you are already at the top does no harm, so it is safe to add as a habit.

About the assignment

Reach the frame by its name, print the heading text that sits in it, then return to the main page.

What you will do

  1. Switch into the frame named nestedFrame with switch_to.frame. Do not use the id frame1 for this one.
  2. Find the h2 element inside the frame and read its text.
  3. Print the text after the label.
  4. Call default_content() so the driver is back on the top document.

Common mistakes

  • Using the wrong frame name

    The first frame is called singleFrame, and the string frame1 is its id. A name that matches nothing raises NoSuchFrameException: nope or similar, with your string in the message.

  • Reading the top document heading

    Without the switch, h2 returns the page title element, whose text is Frames. It prints something, so the mistake is easy to miss.

  • Never leaving the frame

    Later lookups for page elements fail with NoSuchElementException, because the driver is still looking inside the frame.

In a real test suite

Suites avoid raw frame names in tests. A page object exposes a context manager built with with and contextlib, which switches in on entry and calls default_content() in the exit step, even when the test fails.

The next lesson goes further and reads a frame that sits inside another frame.

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 by name nestedFrame, print the h2, then return
finally:
    driver.quit()