0
0
Selenium Pythontesting~15 mins

Nested iFrames in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify content inside nested iFrames
Preconditions (2)
Step 1: Open the web page URL 'https://example.com/nested-iframes'
Step 2: Switch to the outer iFrame using its id 'outer-frame'
Step 3: Within the outer iFrame, switch to the inner iFrame using its name 'inner-frame'
Step 4: Locate the paragraph element with id 'inner-text' inside the inner iFrame
Step 5: Verify that the paragraph text is exactly 'Hello from nested iFrame!'
✅ Expected Result: The paragraph inside the nested inner iFrame displays the text 'Hello from nested iFrame!'
Automation Requirements - Selenium with Python
Assertions Needed:
Assert that the paragraph text inside the nested iFrame matches 'Hello from nested iFrame!'
Best Practices:
Use explicit waits to wait for iFrames and elements to be available
Use By.ID and By.NAME locators for iFrames and elements
Switch back to the default content after test steps
Handle NoSuchFrameException if iFrame is not found
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchFrameException, TimeoutException

def test_nested_iframes():
    driver = webdriver.Chrome()
    wait = WebDriverWait(driver, 10)
    try:
        driver.get('https://example.com/nested-iframes')

        # Wait and switch to outer iFrame by ID
        wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'outer-frame')))

        # Wait and switch to inner iFrame by NAME
        wait.until(EC.frame_to_be_available_and_switch_to_it((By.NAME, 'inner-frame')))

        # Wait for the paragraph element inside inner iFrame
        paragraph = wait.until(EC.visibility_of_element_located((By.ID, 'inner-text')))

        # Assert the paragraph text
        assert paragraph.text == 'Hello from nested iFrame!', f"Expected text not found, got: {paragraph.text}"

    except (NoSuchFrameException, TimeoutException) as e:
        assert False, f"Test failed due to missing frame or element: {e}"
    finally:
        # Always switch back to default content
        driver.switch_to.default_content()
        driver.quit()

if __name__ == '__main__':
    test_nested_iframes()

This test script opens the target web page with nested iFrames.

It uses explicit waits to ensure the outer iFrame is available, then switches to it by its ID 'outer-frame'.

Next, it waits for the inner iFrame by its name 'inner-frame' and switches to it.

Inside the inner iFrame, it waits for the paragraph element with ID 'inner-text' to be visible.

It asserts that the paragraph text exactly matches 'Hello from nested iFrame!'.

If any frame or element is missing, it catches exceptions and fails the test with a clear message.

Finally, it switches back to the main page content and closes the browser to clean up.

Common Mistakes - 4 Pitfalls
Not switching back to default content after working inside iFrames
Using time.sleep() instead of explicit waits
Using incorrect locators like XPath with absolute paths for iFrames
Not handling exceptions when iFrames or elements are missing
Bonus Challenge

Now add data-driven testing to verify paragraph text inside nested iFrames for 3 different URLs and expected texts.

Show Hint