How To Handle Confirmation Pop Up In Selenium WebDriver

Confirmation pop up is also a type of alert pop up. The difference between alert pop up and confirmation pop up is that alert pop up contains single button whereas confirmation pop up contains two buttons “OK” and “CANCEL”. To click on OK button, use accept() method and if you want to click on CANCEL button, use dismiss() method.

Here is my list of best books to learn Selenium.

First of all, we will create a HTML page which displays Confirmation pop up on clicking a button. After that, we will automate it.

Write the following Html code to create a confirmation pop up :

Download this application-Download


Save this script as “popup.htm” and run it to see whether the confirmation pop up comes if we click on button or not.

The page will be something like this :


Now we have created a page which has a confirmation pop up. We can create the script to automate it now. You just need to follow these simple steps :

1) Launch Eclipse, create a project, create a package, and under that package, create a class named “”.

2) Now you need to copy the URL of the  page you created for confirmation pop up. In this case it is “file:///C:/Documents%20and%20Settings/OmS/Desktop/popup.htm”. The line of code you need to write is :

WebDriver driver=new FirefoxDriver();


3) Now you need to click on the “Click Me” button of the web page. For that you need to write one more line of code which is :


We have written it using xpath, but you can use whatever you want in order to locate the particular element. Once you click on the Click Me button, the confirmation pop up will be displayed.

Now if you will directly try to click on “Cancel” or “OK” button, it will not happen because the browser control is still on the parent window. In order to click on the Cancel or Ok button, you need to switch on the child window i.e; you need to bring the browser control from the parent window to the child window.

To bring the browser control from the parent window to the child window, we need to use Alert class. This class contains some methods like accept() and dismiss().

If you want to click on OK button, you need to use the accept() method. But if you want to click on Cancel button, you have to use dismiss() method.

To call these methods, first we need to create an instance of the Alert class. You need to add these following lines of code:

Alert alt=driver.switchTo().alert();

Download this application-Download

The line driver.switchTo().alert() is to bring control from the parent window to the child window.


This is all you need to do in order to work with confirmation pop up…!!!

Here is my list of best books to learn Selenium.

Add Comment