0
0
Selenium Pythontesting~10 mins

iFrame switching (switch_to.frame) in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a webpage containing an iframe, switches the Selenium WebDriver context to the iframe, verifies the presence of a button inside the iframe, clicks it, and then switches back to the main page.

Test Code - Selenium
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
import unittest

class TestIFrameSwitching(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/page_with_iframe')

    def test_switch_to_iframe_and_click_button(self):
        driver = self.driver
        wait = WebDriverWait(driver, 10)

        # Wait for iframe to be present
        iframe = wait.until(EC.presence_of_element_located((By.ID, 'iframe1')))

        # Switch to iframe
        driver.switch_to.frame(iframe)

        # Wait for button inside iframe
        button = wait.until(EC.element_to_be_clickable((By.ID, 'submit-btn')))

        # Click the button
        button.click()

        # Verify button click effect - for example, a success message appears
        success_msg = wait.until(EC.visibility_of_element_located((By.ID, 'success-msg')))
        self.assertTrue(success_msg.is_displayed())

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

        # Verify we are back to main page by checking an element outside iframe
        main_header = wait.until(EC.presence_of_element_located((By.ID, 'main-header')))
        self.assertTrue(main_header.is_displayed())

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 9 Steps
StepActionSystem StateAssertionResult
1Test starts and browser opens the URL 'https://example.com/page_with_iframe'Browser displays the main page with an iframe element with id 'iframe1'-PASS
2Waits until iframe with id 'iframe1' is present in the DOMIframe element is found and ready to switchIframe presence confirmedPASS
3Switches Selenium context to the iframe elementDriver context is now inside the iframe-PASS
4Waits until button with id 'submit-btn' inside iframe is clickableButton inside iframe is visible and enabledButton is clickablePASS
5Clicks the button inside the iframeButton click triggers success message inside iframe-PASS
6Waits for success message with id 'success-msg' to be visible inside iframeSuccess message is displayed inside iframeSuccess message is displayedPASS
7Switches Selenium context back to the main page contentDriver context is now outside iframe, back to main page-PASS
8Waits until main page header with id 'main-header' is presentMain page header is visibleMain header is displayed confirming switch backPASS
9Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: Iframe with id 'iframe1' is not found or not loaded within wait time
Execution Trace Quiz - 3 Questions
Test your understanding
What is the purpose of switching to the iframe in this test?
ATo close the iframe window
BTo interact with elements inside the iframe
CTo refresh the main page
DTo open a new browser tab
Key Result
Always wait for the iframe element to be present before switching context, and switch back to the main content after interacting inside the iframe to avoid stale element errors.