Handling popups in Selenium Java depends on the popup type: JavaScript alerts/prompts/confirms, browser window popups (like authentication or file pickers), or newly opened window/tabs. Here’s how you can handle each scenario with best practices and sample code:
1. Handling JavaScript Alerts, Confirmation, and Prompt Popups
When a JavaScript alert, confirmation box, or prompt appears, use driver.switchTo().alert()
to take control, then interact using the Alert
interface methods like accept()
, dismiss()
, getText()
, or sendKeys()
.
Example: Handling a Simple Alert
import org.openqa.selenium.Alert; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class AlertPopupHandler { public static void main(String[] args) { WebDriver driver = new ChromeDriver(); driver.get("https://the-internet.herokuapp.com/javascript_alerts"); // Assume a button click triggers the alert driver.findElement(By.cssSelector("button[onclick='jsAlert()']")).click(); // Switch to alert Alert alert = driver.switchTo().alert(); // Read and print alert text (optional) System.out.println("Alert text: " + alert.getText()); // Accept the alert (click OK) alert.accept(); driver.quit(); } }
- Use
alert.dismiss()
to cancel/close. - Use
alert.sendKeys("your input")
for prompt dialogs needing input.
2. Handling Browser Window Popups (Multiple Windows or Tabs)
For popups that open in a new tab/window, use getWindowHandles()
to grab all open windows, then loop through and switch control as needed.
Example: Switching to and Closing a New Popup Window
// Store the parent window handle String mainWindow = driver.getWindowHandle(); // Code that triggers popup, e.g., driver.findElement(By.id("popup-link")).click(); Set<String> allWindows = driver.getWindowHandles(); for (String windowHandle : allWindows) { if (!windowHandle.equals(mainWindow)) { driver.switchTo().window(windowHandle); // Now interact with popup elements, for example: driver.close(); // Close the popup window } } // Switch back to the parent window driver.switchTo().window(mainWindow);
3. Handling Permission or Notification Popups
Browser-level permission popups (like notifications) usually require customizing browser settings before launching the driver, since Selenium cannot interact with OS-level or browser UI popups.
Example: Disabling Chrome Notification Popups
import org.openqa.selenium.chrome.ChromeOptions; import java.util.HashMap; import java.util.Map; ChromeOptions options = new ChromeOptions(); Map<String, Object> prefs = new HashMap<>(); prefs.put("profile.default_content_setting_values.notifications", 2); // 1: Allow, 2: Block options.setExperimentalOption("prefs", prefs); WebDriver driver = new ChromeDriver(options);
4. Handling OS-level Dialogs
Selenium cannot handle OS-level (native) dialogs directly, such as file uploads or system-level alerts. Use external tools like AutoIT, Robot class, or third-party libraries for automation in such scenarios.
5. Key Points and Best Practices
- Always switch back to the main window after interacting with popups.
- Use explicit waits where needed to ensure popups have appeared before interacting.
- For JavaScript popups,
driver.switchTo().alert()
is always the entrypoint. - For window/tab popups, loop and switch using
getWindowHandles()
andswitchTo().window()
.
Types of popups you can handle with Selenium:
- JavaScript alerts (accept, dismiss, fetch text, send input)
- New browser windows or tabs
- Modal HTML popups (interact with elements as usual)
- Permission dialogs (with browser options before start)
Selenium WebDriver provides robust methods for browser-based alerts and popups—but not OS-level native dialogs, which need supplemental tools.