Skip to content
CodeKruPro

Read a Nested Frame

51/80

Read a Nested Frame

The child frame is inside the parent. Switch to nestedFrame first, then to frame index 0. You cannot jump straight to the child from the top document.

Task:

  1. Print the parent h2
  2. Switch into the child frame and print the h3
  3. Use parent_frame() and print the parent text again

Print the lines below exactly.

Expected output:

Parent: Parent frame
Child: Child frame
Back to parent: Parent frame

About this lesson

Selenium Python nested iframe: step into a child frame and climb out with parent_frame

Selenium Python AssignmentsAdvancedFrames

How it works

A page can hold an iframe, and that iframe can hold another one. Selenium sees one document at a time, called the current context. Every find_element call searches only there, so a heading two levels down stays hidden until you have switched through both levels.

In Python the entry point is driver.switch_to.frame. It accepts a name or id string, an integer index or a WebElement. The index counts only the frames inside the context you stand in. From the top page, index 0 is the first iframe of the whole page, not the nested one. Inside the parent, the same number means its first child. To go up exactly one level, use driver.switch_to.parent_frame(). At the top it does nothing and raises nothing.

The usual failure is a lookup in the wrong context. It surfaces as NoSuchElementException, which looks like a bad locator.

About the assignment

The Frames page has a frame that contains a second frame. You read one heading at each level and then read the outer heading again, which proves you climbed back up.

What you will do

  1. Switch into the outer frame using the name the lesson gives it, and read its level two heading.
  2. Switch again, this time by position, into the frame inside the outer one, and read its level three heading.
  3. Move up a single level and read the outer heading a second time.
  4. Print the three labelled lines in order, using the .text attribute of each element.

Common mistakes

  • Jumping to the child from the top page

    An index switch from the top document lands in the first iframe of the page, a different document from the one you wanted. Enter the parent first, then the child.

  • Reading the child heading after climbing

    Back in the parent, the level three heading is out of reach. find_element raises NoSuchElementException with "Unable to locate element". The locator is fine; the context is wrong.

  • Using a frame that does not exist

    The parent holds exactly one frame. Index 5 raises NoSuchFrameException, and so does an unknown name, with the name you typed as the only clue.

In a real test suite

In a pytest suite, frame work usually hides inside a page object. A method enters the frame, returns the value, and leaves in a finally block, so tests never track the level.

The next lesson covers the other way out: jumping straight to the top document, however deep you went.

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 parent, the child, then the parent again
finally:
    driver.quit()