0
0
Selenium Pythontesting~15 mins

iFrame switching (switch_to.frame) in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify content inside an iframe on a webpage
Preconditions (2)
Step 1: Open the browser and navigate to 'https://example.com/page-with-iframe'
Step 2: Wait until the iframe with id 'content-frame' is present
Step 3: Switch to the iframe using its id 'content-frame'
Step 4: Locate the heading element inside the iframe with tag 'h1'
Step 5: Verify the heading text is 'Welcome to the iframe content'
Step 6: Switch back to the main page content
Step 7: Locate the main page element with id 'main-title'
Step 8: Verify the main page title text is 'Main Page Title'
✅ Expected Result: The heading inside the iframe matches 'Welcome to the iframe content' and the main page title matches 'Main Page Title'
Automation Requirements - Selenium with Python
Assertions Needed:
Assert the iframe heading text equals 'Welcome to the iframe content'
Assert the main page title text equals 'Main Page Title'
Best Practices:
Use explicit waits to wait for iframe and elements
Use By.ID or By.TAG_NAME locators for clarity
Switch back to default content after iframe interaction
Handle exceptions for element 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 TimeoutException

# Initialize the Chrome WebDriver
with webdriver.Chrome() as driver:
    wait = WebDriverWait(driver, 10)
    driver.get('https://example.com/page-with-iframe')

    try:
        # Wait for iframe to be present
        wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'content-frame')))

        # Now inside iframe, wait for heading
        heading = wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'h1')))

        # Assert heading text inside iframe
        assert heading.text == 'Welcome to the iframe content', f"Expected iframe heading text to be 'Welcome to the iframe content' but got '{heading.text}'"

        # Switch back to main content
        driver.switch_to.default_content()

        # Wait for main page title element
        main_title = wait.until(EC.visibility_of_element_located((By.ID, 'main-title')))

        # Assert main page title text
        assert main_title.text == 'Main Page Title', f"Expected main page title to be 'Main Page Title' but got '{main_title.text}'"

        print('Test passed: iframe and main page content verified successfully.')

    except TimeoutException as e:
        print(f'Test failed: Element not found or timeout occurred - {e}')
    except AssertionError as e:
        print(f'Test failed: Assertion error - {e}')

This script uses Selenium WebDriver with Python to automate the test case.

First, it opens the browser and navigates to the target URL.

It uses an explicit wait to wait until the iframe with id 'content-frame' is available, then switches into it using frame_to_be_available_and_switch_to_it.

Inside the iframe, it waits for the heading <h1> element to be visible, then asserts its text matches the expected string.

After checking the iframe content, it switches back to the main page using switch_to.default_content().

Then it waits for the main page title element by id and asserts its text.

Exceptions for timeouts and assertion failures are caught and printed to help debug.

This approach uses best practices like explicit waits, clear locators, and switching back to default content after iframe interaction.

Common Mistakes - 4 Pitfalls
Not switching back to the main content after interacting with the iframe
Using implicit waits instead of explicit waits for iframe and elements
Using incorrect locator strategies like absolute XPath for iframe or elements
Not handling exceptions for element not found or timeouts
Bonus Challenge

Now add data-driven testing with 3 different iframe ids and expected heading texts

Show Hint