At times in browser,Alert box with a specified message and an OK button is displayed to ensure information comes through to the user.
Alert box prevents the user from accessing other parts of the page until the box is closed. An alert is displayed is as shown below:
While automating the web application, we can handle the pop up using alert Interface
The alert() method displays an alert box with a specified message and an OK button.
Below code shows how to close an window alert pop up using selenium:
The alert() method displays an alert box with a specified message and an OK button.
Below code shows how to close an window alert pop up using selenium:
public void Accept_Alert_Message() {
try {
//Using webdriver wait, we will wait for the alert
//to appear before performing action on the alert
WebDriverWait driverwait = new WebDriverWait(driver, 5);
driverwait.until(ExpectedConditions.alertIsPresent());
//switch to the alert element
Alert alert = driver.switchTo().alert();
//accept the alert displayed
alert.accept();
//switch to the default content
driver.switchTo().defaultContent();
} catch (Exception e) {
// in case alert is not displayed, the code will go to catch loop
}
}