In this section we will discuss on Alert in Selenium. We have focused on the details about the file uploading and downloading with Selenium. Also discussed about how to achieve this scenario with multiple ways like sendKeys(). AutoIT and Jacob API.
Now in this section we will focus on the handling strategy of Alert popup box in selenium WebDriver.
Alert is a popup that comes up on the screen. These are different from the regular window and its blocking he main window for next operation. If you will access the main window’s content even if popup is open then you may get an exception called UnhandledAlertException which is a modal dialog.
There are two type of alert popup we should be focusing in automation testing.
Windows Based
Web Based(Browser Based)
In this section we will concentrate on the Web Based Alert Popup.
Web Based Alert Popup:
From the name you can guess the definition of the Web Based Alert Popup.
Yes. This is basically dealing with the web page. This popup may display on clicking any link on the page or on any action which need acknowledgement etc.
For example if a link requires authentication to view the page then it may ask you for the user name and password. Until you close or provide the user name and password you will not able to access that page or the main window.
So, we have to handle the popup in automation testing.
Types of Alerts
We have 3 types of alert in web based popup. Below is the list of Alert type
Simple Alert
Confirmation Alert
Prompt alert
Simple Alerts
This alerts displays information and warning on the screen. There is some text in the popup along with the OK button. Until you click on the OK button or close the popup you will not able to access the main window.

Prompt Alert
This alert needs some input from user. There is some text in popup along with text box and 2 button (Ok and Cancel). You need to pass some value to the text box and click the OK Button to close the popup or click on the cancel button.

Confirmation Alert:
This confirmation alert require permission to perform the action. You need to either accept or decline to the popup to handle with the popup.

How to handle Alert in Selenium WebDriver:
First thing you need to remember that Alert is an Interface. It is present in org.openqa.selenium.Alert package. This provides some methods which are used in Selenium WebDriver. Below are some of the methods. You can switch from main window to popup window by using the .switchTo() method.
dismiss():
This method is used to click on the Cancel button of the alert. Below is the example of implementation of dismiss() in selenium.
Alert alert = driver.switchTo.alert().dismiss();
accept();
This method is used to click on the OK button of the alert. Below is the example of the implementation of accept() in selenium WebDriver.
Alert alert = driver.switchTo.alert().accept();
getText();
This method is used to capture the text of the alert. Below is the example of the implementation of accept() in selenium WebDriver.
Alert alert = driver.switchTo.alert().getText();
sendKeys();
This method is used to send some data to the alert. Below is the example of the implementation of accept() in selenium WebDriver.
Alert alert = driver.switchTo.alert().sendKeys(“Text”);
We will learn the handling window based popup in upcoming section.