Handling Alert Popup
Explanation of key steps:
- Setup & launch – set the webdriver.chrome.driver system property and instantiate ChromeDriver.
2. Navigate – open the page that contains buttons to trigger various JavaScript alerts.
3. Trigger alerts – click the buttons that fire a simple alert, a confirmation dialog, and a prompt dialog.
4. Wait & switch – use WebDriverWait with ExpectedConditions.alertIsPresent() to pause until the alert appears, then driver.switchTo().alert() returns an Alert object.
5. Interact –
- Simple alert: accept() to click “OK.”
- Confirmation alert: dismiss() to click “Cancel.”
- Prompt alert: sendKeys(…) to enter text, then accept().
6. Teardown – always wrap your logic in a try…finally and call driver.quit() to ensure the browser closes even if an exception occurs.
Adjust the URL, locators (By.xpath(…)), and button IDs (alertButton, confirmButton, promptButton) to match your application under test.
Program:
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class AlertHandlingExample {
public static void main(String[] args) throws InterruptedException {
// 1. Point to your ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// 2. Launch Chrome
WebDriver driver = new ChromeDriver();
try {
// 3. Navigate to the page with alerts
driver.get("https://example.com/alerts");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
// --- SIMPLE ALERT ---
// 4. Trigger a simple alert
WebElement simpleAlertBtn = driver.findElement(
By.xpath("//button[@id='alertButton']"));
simpleAlertBtn.click();
// 5. Wait for and switch to the alert
Alert simpleAlert = wait.until(
ExpectedConditions.alertIsPresent());
// 6. Read and print its message
System.out.println("Simple alert says: " + simpleAlert.getText());
// 7. Accept the alert (click OK)
simpleAlert.accept();
Thread.sleep(1000);
// --- CONFIRMATION ALERT ---
// 8. Trigger a confirmation alert
WebElement confirmAlertBtn = driver.findElement(
By.xpath("//button[@id='confirmButton']"));
confirmAlertBtn.click();
Alert confirmAlert = wait.until(
ExpectedConditions.alertIsPresent());
System.out.println("Confirm alert says: " + confirmAlert.getText());
// 9. Dismiss the alert (click Cancel)
confirmAlert.dismiss();
Thread.sleep(1000);
// --- PROMPT ALERT ---
// 10. Trigger a prompt alert
WebElement promptAlertBtn = driver.findElement(
By.xpath("//button[@id='promptButton']"));
promptAlertBtn.click();
Alert promptAlert = wait.until(
ExpectedConditions.alertIsPresent());
System.out.println("Prompt alert says: " + promptAlert.getText());
// 11. Send text to the prompt and accept
promptAlert.sendKeys("Selenium Test");
promptAlert.accept();
Thread.sleep(1000);
} finally {
// 12. Close the browser (end the session)
driver.quit();
}
}
}