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
- Switch into the frame named
nestedFramewithswitch_to.frame. Do not use the idframe1for this one. - Find the
h2element inside the frame and read its text. - Print the text after the label.
- 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 stringframe1is its id. A name that matches nothing raisesNoSuchFrameException: nopeor similar, with your string in the message.Reading the top document heading
Without the switch,
h2returns 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.
Related lessons
- Enter a Frame by ID with switch_to.frameUses the same method with an id instead of a name.
- Read a Nested FrameReads a frame nested inside another one.
- Return to the Top DocumentPractises returning to the top document.
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")
# Switch by name nestedFrame, print the h2, then return
finally:
driver.quit()