0
0
Selenium Pythontesting~10 mins

Default content switching in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test verifies that after switching to an iframe and interacting with elements inside it, the test correctly switches back to the main page (default content) to interact with elements outside the iframe.

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 TestDefaultContentSwitching(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/page_with_iframe')
        self.wait = WebDriverWait(self.driver, 10)

    def test_switch_to_iframe_and_back(self):
        driver = self.driver
        wait = self.wait

        # Wait for iframe and switch to it
        iframe = wait.until(EC.presence_of_element_located((By.ID, 'iframe1')))
        driver.switch_to.frame(iframe)

        # Inside iframe: find button and click
        button_in_iframe = wait.until(EC.element_to_be_clickable((By.ID, 'btnInsideIframe')))
        button_in_iframe.click()

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

        # On main page: find input and enter text
        input_main = wait.until(EC.presence_of_element_located((By.ID, 'mainInput')))
        input_main.send_keys('Test input')

        # Assert input value
        self.assertEqual(input_main.get_attribute('value'), 'Test input')

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

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 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 for iframe with id 'iframe1' to be present and switches to itDriver context is inside the iframe contentIframe element is located and switched toPASS
3Waits for button with id 'btnInsideIframe' inside iframe and clicks itButton inside iframe is clickedButton is clickable and click action performedPASS
4Switches back to default content (main page)Driver context is back to main page contentDriver is no longer inside iframePASS
5Waits for input with id 'mainInput' on main page and enters text 'Test input'Input field on main page contains text 'Test input'Input field value equals 'Test input'PASS
6Test ends and browser closesBrowser closed-PASS
Failure Scenario
Failing Condition: Test fails if the driver does not switch back to default content before interacting with main page elements
Execution Trace Quiz - 3 Questions
Test your understanding
Why does the test call driver.switch_to.default_content() after clicking the button inside the iframe?
ATo refresh the iframe content
BTo close the iframe
CTo switch back to the main page so elements outside the iframe can be accessed
DTo switch to another iframe
Key Result
Always switch back to the main page (default content) after working inside an iframe before interacting with elements outside it to avoid element not found errors.