What is Alert ?
Alert is a small message box that displays on-screen notifications to give the user some kind of information or asks for permission to perform some type of operation. It can also be used for warning purposes.
Type of Operation’s that we can perform on Alert :
- accept() : To accept the alert.
- dismiss() : To dismiss the alert.
- getText() : To get the text of the alert.
- sendKeys() : To write some text to the alert.
There are 3 types of Alert box :
- Simple Alert box : It give the some information about the current screen or give some warning.
- Prompt Dialogue Alert box : It Ask from the User to provide some inputs.
- Confirm Alert : This Alert box ask for a permission to perform some type of operation.

Example of Handle Alert and Popup Using selenium :
package automationTestingLab;
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;
public class Handle_Alert {
public static void main(String[] args) {
/*
* Manual Actions on Alert box :-
* To accept the alert.
* To dismiss the alert.
* To get the text of the alert.
* To write some text to the alert.
*/
System.setProperty("webdriver.chrome.driver","F:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.seleniumeasy.com/test/javascript-alert-box-demo.html");
// Click the button to show Alert box
WebElement alertWithOK = driver.findElement(By.xpath("//button[@class='btn btn-default']"));
alertWithOK.click();
// Switch to Alert to perform some operation
Alert simpleAlertBox = driver.switchTo().alert();
// GetText of alert
String alertBoxText = driver.switchTo().alert().getText();
System.out.println(alertBoxText);
// Accept alert
simpleAlertBox.accept();
// Click the button to show Alert with ok box
WebElement alertWithOKAndCancel = driver.findElement(By.xpath("//button[@class='btn btn-default btn-lg'][contains(text(),'Click me!')]"));
alertWithOKAndCancel.click();
// Switch to Alert to perform some operation
Alert confirmAlert = driver.switchTo().alert();
// Dismiss alert
confirmAlert.dismiss();
// Click the button to show Alert with text box
WebElement alertWithTextBox = driver.findElement(By.xpath("//button[contains(text(),'Click for Prompt Box')]"));
alertWithTextBox.click();
// Switch to Alert to perform some operation
Alert promptDialogueAlertBox = driver.switchTo().alert();
// SendKeys to particular alert
promptDialogueAlertBox.sendKeys("== Type on alert box and accept it ==");
promptDialogueAlertBox.accept(); //Accept or dismiss after send message
}
}