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.
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.
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()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test 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 |
| 2 | Waits until iframe with id 'iframe1' is present in the DOM | Iframe element is found and ready to switch | Iframe presence confirmed | PASS |
| 3 | Switches Selenium context to the iframe element | Driver context is now inside the iframe | - | PASS |
| 4 | Waits until button with id 'submit-btn' inside iframe is clickable | Button inside iframe is visible and enabled | Button is clickable | PASS |
| 5 | Clicks the button inside the iframe | Button click triggers success message inside iframe | - | PASS |
| 6 | Waits for success message with id 'success-msg' to be visible inside iframe | Success message is displayed inside iframe | Success message is displayed | PASS |
| 7 | Switches Selenium context back to the main page content | Driver context is now outside iframe, back to main page | - | PASS |
| 8 | Waits until main page header with id 'main-header' is present | Main page header is visible | Main header is displayed confirming switch back | PASS |
| 9 | Test ends and browser closes | Browser window closed | - | PASS |