</>

Technology

Selenium

Difficulty

Intermediate

Interview Question

How do you handle iframes, multiple windows, and alerts in Selenium?

Answer

iFrames, Multiple Windows & Alerts in Selenium

1. Handling iFrames

An iframe embeds a separate HTML document. Selenium must switch context to interact inside it.

Java
// Switch by index (0-based)
driver.switchTo().frame(0);

// Switch by name or id
driver.switchTo().frame("paymentFrame");

// Switch by WebElement
WebElement iFrame = driver.findElement(By.cssSelector("iframe.payment-widget"));
driver.switchTo().frame(iFrame);

// Now interact inside the iframe
driver.findElement(By.id("card-number")).sendKeys("4111111111111111");
driver.findElement(By.id("cvv")).sendKeys("123");

// Switch BACK to main page (parent frame)
driver.switchTo().defaultContent();   // goes to top-level
driver.switchTo().parentFrame();      // goes to immediate parent (for nested iframes)
Java
// Nested iframes
driver.switchTo().frame("outer-frame");
driver.switchTo().frame("inner-frame");   // frame within frame
driver.findElement(By.id("inside-nested")).click();
driver.switchTo().defaultContent();       // back to main

2. Multiple Windows / Tabs

Java
// Get current window handle (before opening new window)
String mainWindow = driver.getWindowHandle();

// Click something that opens a new window/tab
driver.findElement(By.id("open-popup")).click();

// Get all window handles
Set<String> allWindows = driver.getWindowHandles();

// Switch to new window
for (String handle : allWindows) {
    if (!handle.equals(mainWindow)) {
        driver.switchTo().window(handle);
        break;
    }
}

// Perform actions in new window
System.out.println("Popup title: " + driver.getTitle());
driver.findElement(By.id("confirm-btn")).click();

// Close popup window and switch back
driver.close();                             // closes CURRENT window
driver.switchTo().window(mainWindow);       // switch back to main

// Open new tab (Selenium 4)
driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://example.com");

// Open new window (Selenium 4)
driver.switchTo().newWindow(WindowType.WINDOW);

3. Alerts (JavaScript Popups)

Java
// Wait for alert and switch to it
Alert alert = wait.until(ExpectedConditions.alertIsPresent());

// OR immediately switch (if you know it's there)
Alert alert = driver.switchTo().alert();

// Get alert message
String message = alert.getText();
System.out.println("Alert says: " + message);

// Accept (OK)
alert.accept();

// Dismiss (Cancel)
alert.dismiss();

// Prompt — type text then accept
alert.sendKeys("My input text");
alert.accept();
Java
// Handling alert types:

// 1. Simple alert — just OK button
driver.findElement(By.id("show-alert")).click();
driver.switchTo().alert().accept();

// 2. Confirm — OK or Cancel
driver.findElement(By.id("show-confirm")).click();
Alert confirm = driver.switchTo().alert();
System.out.println(confirm.getText());   // "Are you sure?"
confirm.accept();    // clicks OK
// confirm.dismiss(); // clicks Cancel

// 3. Prompt — text input + OK/Cancel
driver.findElement(By.id("show-prompt")).click();
Alert prompt = driver.switchTo().alert();
prompt.sendKeys("John Doe");
prompt.accept();

Complete Window Management Helper

Java
// utility method — switch to window by title
public void switchToWindowByTitle(String targetTitle) {
    String currentHandle = driver.getWindowHandle();
    Set<String> handles  = driver.getWindowHandles();

    for (String handle : handles) {
        driver.switchTo().window(handle);
        if (driver.getTitle().contains(targetTitle)) {
            return;  // found it — stay here
        }
    }
    // Not found — switch back to original
    driver.switchTo().window(currentHandle);
    throw new NoSuchWindowException("Window with title '" + targetTitle + "' not found");
}

// Usage
switchToWindowByTitle("Payment Gateway");
driver.findElement(By.id("pay-btn")).click();

Follow AutomateQA

Related Topics