How to Handle Alert and Popup in Selenium WebDriver

In this post will learn how to handle Alert and Popup in Selenium WebDriver. Check out the steps.

What Is Alert ?

Alert is a message box which is usually put up inside any form or login page.
There are two clickable options in alert box “OK” or “Cancel”.

In this post I will explain you how to automate alert box or how to click on OK or cancel using selenium web driver.

Steps to Handle Alert and Popup in Selenium WebDriver:

— Create new package or add new class to the existing package
(Eclipse toolbar -> New -> Package -> Give package name)
— To learn more about how to start with creating project, Jar files and create class CLICK HERE

Let’s take an example of , there is a login page on this button, we will proceed with automating following steps:
1. Open URL
2. Scroll down the page to login screen
3. Enter Username “ABC”
4. Click on “Login” button.
5. Pop up will appear
6. Click OK button on popup.

switchto().alert().accept() is a method which we can use to redirect selenium to identify alert message and to identify the buttons in alert box if it’s OK or cancel. alert().accept() will click OK button and dismiss() is used to click on cancel button. You can also use implicit or explicit wait page takes a lot of time for the pop up to appear. These waits will wait for the defined time or expected condition before giving any exception.

Advantage of Selenium:

Major advantage of selenium for web application over appium is that there is no need to write any code for making the web page scroll able we have to just give the xpath, id or class name of an element. Selenium with automatically scroll page and find out the element on page, if the particular element is available.

Code for this solution is given below:

[php light=”true”]
package system.admin;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Alertbox {

public static void main(String[] args)

WebDriver driver = new FirefoxDriver();
System.out.println("Launching Browser");

// window.maximize() is used to maximize window

// sendkeys method is used to enter text inside textbox

// Click on element using it’s xpath

// switchto().alert().accept() is used to click on OK button on alert box



Code will look like this in eclipse:

Watch below video to understand how the execution will take place after running this code.

Method used to click on cancel button on alert box:

Switchto().alert().dismiss() method is used to click on cancel button in alert box.
This method will find alert box on page and will click on cancel button. With the help of this method any pop up could be handled whether it’s a java scrip or customized Pop-Up, You can refer our other post where you can learn the next level for inserting text inside the Pop-Up.
Switchto() method is used to redirect to the website Pop-Up.

Leave a Reply

Your email address will not be published. Required fields are marked *