Alert Handling in Selenium
Master alert popup handling including simple alerts, confirmation alerts, prompt alerts, and authentication popups using Alert interface.
Alert Handling in Selenium
Why Handle Alerts?
We handle Alert Pop up in Selenium by using Alert Interface. Basically we can't inspect the alert pop up elements, so we have to use Alert interface methods to handle the alert pop up objects/elements.
🔑 Key Points
- • Alert elements cannot be inspected in browser developer tools
- • Must switch focus from main window to alert window
- • Use Alert interface methods to interact with alert elements
- • Different types of alerts require different handling approaches
Alert Interface Methods
Switch Focus to Alert
accept() Method
Click on OK button
dismiss() Method
Click on Cancel button
getText() Method
Capture alert text
sendKeys() Method
Enter value in alert textbox
Types of Alert Popups
1. Simple Alert Popup
Components:
- • Alert popup text
- • OK button
Example: https://mail.rediff.com/cgi-bin/login.cgi
Click Login button without entering credentials to trigger alert
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class Demo1 {
public static void main(String[] args) {
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
// Locate Log In Button and click on it
driver.findElement(By.name("proceed")).click();
// Switch focus from Main window to Alert Pop Up window
Alert alt = driver.switchTo().alert();
// Capture Alert Pop Text
String a = alt.getText();
System.out.println(a);
// Click on Alert Pop Up OK button
alt.accept();
}
}
2. Confirmation Alert Popup
Components:
- • Alert popup text
- • OK button
- • Cancel button
Example: https://demoqa.com/alerts
Click on 3rd "Click Me" button (confirmButton)
driver.get("https://demoqa.com/alerts");
// Find and click on 3rd Click Me button
driver.findElement(By.id("confirmButton")).click();
// Switch focus from main window to alert window
Alert alt = driver.switchTo().alert();
// Capture alert pop up text
String a = alt.getText();
System.out.println(a);
// Click on Cancel button
alt.dismiss();
3. Prompt Alert Popup
Components:
- • Alert popup text
- • OK button
- • Cancel button
- • Alert popup text box
Example: https://demoqa.com/alerts
Click on 4th "Click Me" button (promtButton)
driver.get("https://demoqa.com/alerts");
// Find and click on 4th click me button
driver.findElement(By.id("promtButton")).click();
// Switch focus main window to alert window
Alert alt = driver.switchTo().alert();
// Capture the alert pop up text
String a = alt.getText();
System.out.println(a);
// Enter value in alert pop
alt.sendKeys("Durga");
Thread.sleep(4000);
// Click on ok button
alt.accept();
4. Authentication Alert Popup
Components:
- • Username text box
- • Password text box
- • Sign In button
- • Cancel button
🔐 Special Handling
Authentication alerts that appear immediately after opening URL are handled by providing credentials in the URL itself.
Example: Basic Authentication
import org.openqa.selenium.chrome.ChromeDriver;
public class Demo4 {
public static void main(String[] args) {
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
// Handle authentication by embedding credentials in URL
driver.get("https://admin:admin@the-internet.herokuapp.com/basic_auth");
}
}
switchTo() Method Overview
WebDriver switchTo() Capabilities
switchTo() Method Usage
switchTo() method is present in WebDriver Interface and returns TargetLocator interface. It's used to switch focus to:
- • Alert window
- • Frame window
- • New window/tab
- • Different tabs or windows
Interface Details
Alert Interface Methods
Practice URLs
Alert Practice Websites
Simple & Confirmation Alerts
- • https://demo.automationtesting.in/Alerts.html
- • https://demoqa.com/alerts
- • https://mail.rediff.com/cgi-bin/login.cgi
- • https://rahulshettyacademy.com/AutomationPractice/
Authentication Alerts
- • https://the-internet.herokuapp.com/basic_auth
- • https://admin:admin@the-internet.herokuapp.com/basic_auth