How to Handle Alerts in webpage
What is a JavaScript Alert?
Alerts are native browser popups (alert, confirm, or prompt boxes) that WebDriver cannot interact with using normal element locators — they require the dedicated Alert interface.
Handling an Alert
Alert alert = driver.switchTo().alert();
String text = alert.getText();
alert.accept(); // clicks OK
alert.dismiss(); // clicks Cancel
alert.sendKeys("input text"); // for prompt boxes
Why This Matters
Attempting to interact with the page before handling an open alert will throw an exception, so alerts must always be switched to and resolved first.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.
.png)