How To Handle Alert Pop Up In Selenium WebDriver

In Selenium there are different kinds of Pop – ups which will behave in different ways. To handle these types of pop – ups, it completely depends on the properties of the pop – ups. We have following types of pop-ups:

1) Alert Pop Up

2) Confirmation Pop Up

3) Hidden-Division Pop Up

4) Calendar Pop Up

5) Child Browser Pop Up

6) Page On Load Pop Up

7) Download Pop UP

8) Upload Pop Up

Here we will be discussing about the alert pop up. Whenever we are working with alert pop up the control of the browser will be behind the pop up. In order to switch the control to alert pop up use the following command :


The return type of alert() method is Alert. In order to fetch the text present on the pop up, use getText() method. In order to click on OK button, use accept() method.

Example :

1) Launch Eclipse and create a class under the package of a project. Name the class as “tstcls”.


2) When you open a website, the control of the browser is on the current window. In case of alert, when a alert will pop up, the control of the browser will still be on the parent window. So to perform any operation on the alert, we need to switch the control of the browser from the parent window to the child window i.e; alert window. To do this, see the following code :

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


3) Here you can see that alert() method will help you to switch on the alert window. The alert window will have two options, those are OK button and one cancel cross mark. In order to click on the OK button, we have to use accept() method.



Properties Of Alert Pop Up :

1) You can move the pop up.

2) It has only one button “OK”.

3) It is in black and white, very dull.

NOTE : In alert pop up when we switch the control from web page to alert pop up, as soon as OK button is clicked, the control is transferred back to web page automatically.

Add Comment