Handling Popups and Alerts in Selenium
- swathiperiasamy
- Sep 30
- 3 min read
Updated: Oct 6

Selenium is one of the most popular open-source automation tools for web applications. It allows testers to simulate user actions like clicking buttons, filling forms, navigating pages, and handling complex UI interactions.
In real-world apps, alerts and popups are very common, like;
Banking: confirmation alert before money transfer
E-commerce: login popup before checkout
Government portal: file upload dialog
If these aren’t handled, automation scripts will fail. That’s why popup handling is essential for Selenium testers.
Alerts in Selenium
Now, let see what are the types of Alerts in Selenium.
Simple Alert – It only contains a message and an OK button. It is usually used to display information or warnings to the user.
Confirmation Alert – Provides OK and Cancel options. It is generally used when an application needs user confirmation before performing an action.
Prompt Alert – Allows the user to enter some input before proceeding. It contains a text box along with OK and Cancel buttons.
Selenium Methods for Handling Alerts;
accept() → Click OK
dismiss() → Click Cancel
getText() → Get alert message
sendKeys() → Enter text in prompt
Real-Time Example: Handling Alerts
We’ll use the Herokuapp demo site to handle all three alert types:
import org.openqa.selenium.Alert;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;public class AlertHandlingExample { public static void main(String[] args) { WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://the-internet.herokuapp.com/javascript_alerts"); // --- Simple Alert --- driver.findElement(By.xpath("//button[text()='Click for JS Alert']")).click(); Alert simpleAlert = driver.switchTo().alert(); System.out.println("Simple Alert: " + simpleAlert.getText()); simpleAlert.accept() // --- Confirmation Alert --- driver.findElement(By.xpath("//button[text()='Click for JS Confirm']")).click(); Alert confirmAlert = driver.switchTo().alert(); System.out.println("Confirmation Alert: " + confirmAlert.getText()); confirmAlert.dismiss(); // Click Cancel // --- Prompt Alert --- driver.findElement(By.xpath("//button[text()='Click for JS Prompt']")).click(); Alert promptAlert = driver.switchTo().alert(); promptAlert.sendKeys("Selenium Tester"); System.out.println("Prompt Alert: " + promptAlert.getText()); promptAlert.accept(); driver.quit(); }}How it works:
switchTo().alert() → Focus moves to the alert
accept() → OK, dismiss() → Cancel, sendKeys() → Enter text
getText() → Print alert message for validation
Popups in Selenium
Popups are windows or messages that appear on top of the main webpage. They usually require the user’s attention before they can continue interacting with the site. In real-world applications, you’ll come across different types of popups — for login, file upload/download, advertisements, confirmation boxes, etc. Unlike JavaScript alerts (which Selenium can handle using driver.switchTo().alert()), popups can be of different types and sometimes need special handling.
Types of Popups
Browser Popups (Window-based)
Triggered by actions like opening a new window/tab.
Example: Clicking a “Help” link opens in a new tab.
Handled by switching between window handles.
String main = driver.getWindowHandle();for (String h : driver.getWindowHandles()) { driver.switchTo().window(h);}driver.close();driver.switchTo().window(main);File Upload Popup
When a site asks you to upload a file, a system file chooser opens.
Selenium cannot handle OS-level popups, but if the file input is HTML-based, you can use:
driver.findElement(By.id("fileUpload")).sendKeys("C:\\path\\file.txt");File Download Popup
Sometimes clicking a download link opens a system dialog.
Selenium cannot handle OS-level dialogs directly.
Instead, you configure the browser (ChromeOptions, Firefox Profile) to auto-download files to a specific folder.
driver.findElement(By.id("loginModal")).click();Web-Based Modal Dialogs
These look like popups but are actually part of the webpage (HTML elements).
Example: Login/Register popup.
Can be handled like normal elements using locators.
Real-Time Example: Multiple Windows
driver.get("https://the-internet.herokuapp.com/windows");driver.findElement(By.linkText("Click Here")).click();String main = driver.getWindowHandle();for (String h : driver.getWindowHandles()) { if (!h.equals(main)) { driver.switchTo().window(h); System.out.println(driver.getTitle()); driver.close(); }}driver.switchTo().window(main);Conclusion
Alerts → switchTo().alert()
Popups → window handles, locators, or browser configurations
File uploads → sendKeys for HTML inputs
File downloads → configure browser preferences
Mastering alerts and popups ensures Selenium tests are robust, reliable, and interview-ready.


