Test Overview
This test opens a webpage containing an iframe, switches the Selenium WebDriver context to the iframe, verifies a button inside the iframe is clickable, 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 a button inside the iframe is clickable, clicks it, and then switches back to the main page.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; public class IFrameSwitchTest { WebDriver driver; WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testSwitchToIFrameAndClickButton() { driver.get("https://example.com/page-with-iframe"); // Wait for iframe to be present and switch to it wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iframe1"))); // Now inside iframe context WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("btnInsideIframe"))); assertTrue(button.isDisplayed(), "Button inside iframe should be visible"); button.click(); // Switch back to main page driver.switchTo().defaultContent(); // Verify we are back by checking an element outside iframe WebElement header = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("mainHeader"))); assertTrue(header.isDisplayed(), "Main page header should be visible after switching back"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is blank, ready to navigate | — | PASS |
| 2 | Navigate to https://example.com/page-with-iframe | Page loads with main content and an iframe with id 'iframe1' | Page loaded successfully | PASS |
| 3 | Wait until iframe with id 'iframe1' is available and switch to it | Driver context is now inside the iframe | Iframe is present and switched to | PASS |
| 4 | Wait for button with id 'btnInsideIframe' to be clickable inside iframe | Button is visible and enabled inside iframe | Button is displayed (assertTrue passes) | PASS |
| 5 | Click the button inside iframe | Button click triggers expected action inside iframe | — | PASS |
| 6 | Switch back to main page content using driver.switchTo().defaultContent() | Driver context is back to main page | Context switched back to main page | PASS |
| 7 | Wait for element with id 'mainHeader' to be visible on main page | Main page header is visible | Header is displayed (assertTrue passes) | PASS |
| 8 | Test ends and browser closes | Browser window closed | — | PASS |
driver.switchTo().frame() do in Selenium WebDriver?switchTo().frame()driver.switchTo().frame(), where frame() accepts name, index, or WebElement.frame() switches to that iframe by name or ID.driver.switchTo().frame(0);
String text = driver.findElement(By.id("insideFrame")).getText();
driver.switchTo().defaultContent();
String mainText = driver.findElement(By.id("outsideFrame")).getText();text and mainText contain?findElement(By.id("insideFrame")) searches inside that iframe.defaultContent() switches focus back to main page, so findElement(By.id("outsideFrame")) searches main page.driver.switchTo().frame("frame1");
driver.findElement(By.id("button")).click();
driver.switchTo().frame("frame2");
driver.findElement(By.id("link")).click();defaultContent(), then switch to the second iframe.Pass the test and this pattern moves unknown