Alerts, Frames & Multiple Windows
Handle JavaScript alerts, switch between iframes, manage multiple browser windows/tabs, and interact with embedded content.
Alerts, Frames & Windows
Web applications use alerts for confirmations, iframes for embedded content, and new windows/tabs for additional views. Selenium needs explicit switching to interact with each of these.
Alerts & Dialogs
JavaScript alerts, confirms, and prompts require switching to the alert context before interacting. Use driver.switchTo().alert() to accept, dismiss, read text, or type into prompts.
Frames & iFrames
Content inside a frame is in a separate document context. You must switch into the frame before finding elements inside it, then switch back to the main page when done.
Multiple Windows & Tabs
Selenium 4 introduces newWindow() to create new tabs or windows directly. Use window handles to switch between them.
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.*;
import org.testng.Assert;
import org.testng.annotations.*;
import java.time.Duration;
import java.util.Set;
public class AlertsFramesWindowsTest {
WebDriver driver;
@BeforeMethod
public void setup() {
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@Test
public void testJavaScriptAlert() {
driver.get("https://www.selenium.dev/selenium/web/alerts.html");
driver.findElement(By.id("alert")).click();
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println("Alert text: " + alertText);
alert.accept();
}
@Test
public void testConfirmDialog() {
driver.get("https://www.selenium.dev/selenium/web/alerts.html");
driver.findElement(By.id("confirm")).click();
Alert confirm = driver.switchTo().alert();
System.out.println("Confirm text: " + confirm.getText());
confirm.dismiss(); // Click Cancel
// confirm.accept(); // Click OK
}
@Test
public void testPromptDialog() {
driver.get("https://www.selenium.dev/selenium/web/alerts.html");
driver.findElement(By.id("prompt")).click();
Alert prompt = driver.switchTo().alert();
prompt.sendKeys("Selenium 4");
prompt.accept();
}
@Test
public void testIframeSwitch() {
driver.get("https://www.selenium.dev/selenium/web/iframes.html");
// Switch by index
driver.switchTo().frame(0);
// Now you can interact with elements inside the iframe
// Switch back to main page
driver.switchTo().defaultContent();
// Switch by name or id
// driver.switchTo().frame("myFrame");
// Switch by WebElement
// WebElement frame = driver.findElement(By.cssSelector("iframe"));
// driver.switchTo().frame(frame);
}
@Test
public void testNewWindow_Selenium4() {
driver.get("https://www.selenium.dev/");
String originalWindow = driver.getWindowHandle();
// Selenium 4: Open a new tab
driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://www.selenium.dev/documentation/");
Assert.assertEquals(driver.getWindowHandles().size(), 2);
// Switch back to original
driver.switchTo().window(originalWindow);
Assert.assertTrue(driver.getCurrentUrl().contains("selenium.dev"));
}
@Test
public void testMultipleWindows() {
driver.get("https://www.selenium.dev/selenium/web/window_switching_tests/page_with_frame.html");
String mainWindow = driver.getWindowHandle();
// Click a link that opens new window
// ... (action that opens new window)
Set<String> handles = driver.getWindowHandles();
for (String handle : handles) {
if (!handle.equals(mainWindow)) {
driver.switchTo().window(handle);
System.out.println("New window title: " + driver.getTitle());
driver.close(); // Close this window
}
}
driver.switchTo().window(mainWindow);
}
@AfterMethod
public void teardown() { if (driver != null) driver.quit(); }
}
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 pytest
@pytest.fixture
def driver():
d = webdriver.Chrome()
d.implicitly_wait(10)
yield d
d.quit()
def test_javascript_alert(driver):
driver.get("https://www.selenium.dev/selenium/web/alerts.html")
driver.find_element(By.ID, "alert").click()
alert = driver.switch_to.alert
print(f"Alert text: {alert.text}")
alert.accept()
def test_confirm_dialog(driver):
driver.get("https://www.selenium.dev/selenium/web/alerts.html")
driver.find_element(By.ID, "confirm").click()
confirm = driver.switch_to.alert
print(f"Confirm text: {confirm.text}")
confirm.dismiss() # Click Cancel
def test_prompt_dialog(driver):
driver.get("https://www.selenium.dev/selenium/web/alerts.html")
driver.find_element(By.ID, "prompt").click()
prompt = driver.switch_to.alert
prompt.send_keys("Selenium 4")
prompt.accept()
def test_iframe_switch(driver):
driver.get("https://www.selenium.dev/selenium/web/iframes.html")
# Switch by index
driver.switch_to.frame(0)
# Switch back to main page
driver.switch_to.default_content()
# Switch by name/id: driver.switch_to.frame("myFrame")
# Switch by element: driver.switch_to.frame(element)
def test_new_window_selenium4(driver):
"""Selenium 4: newWindow API"""
driver.get("https://www.selenium.dev/")
original = driver.current_window_handle
# Open new tab
driver.switch_to.new_window('tab')
driver.get("https://www.selenium.dev/documentation/")
assert len(driver.window_handles) == 2
# Switch back
driver.switch_to.window(original)
assert "selenium.dev" in driver.current_url
def test_multiple_windows(driver):
driver.get("https://www.selenium.dev/")
main_window = driver.current_window_handle
# Open new window
driver.switch_to.new_window('window')
driver.get("https://www.selenium.dev/documentation/")
for handle in driver.window_handles:
if handle != main_window:
driver.switch_to.window(handle)
print(f"New window title: {driver.title}")
driver.close()
driver.switch_to.window(main_window)
Written by PV
© 2026 All Rights Reserved