How To Handle Javascript Alerts/PopUps In Selenium WebDriver


In this post, we see how to handle javascript alerts/popus. Alerts are basically popup boxes that take your focus away from the current browser and forces you to read the alert message. You need to do some action such as accept or dismiss the alert box to resume your task on the browser.


To handle alerts popupswe need to do switch to the alert window and call Selenium WebDriver Alert API methods.
There are two types of alerts.
  1. Windows Based
  2. Web Based/Browser Based
Here in this post, I confine to Java Script Alerts (A.K.A. Browser/Web Based Alerts).
For Windows Based, Please check the below link.
To handle Browser based Alerts (Web based alert popups), we use Alert Interface. The Alert Interface provides some methods to handle the popups.
While running the WebDriver script, the driver control will be on the browser even after the alert generated which means the driver control will be behind the alert pop up. In order to switch the control to alert pop up, we use the following command :
driver.switchTo().alert();
Once we switch the control from browser to the alert window. We can use the AlertInterface methods to do required actions such as accepting the alert, dismissing the alert, get the text from the alert window, writing some text on the alert window etc.,
Let’s see the Alert Interface Methods.
We need to Import a package org.openqa.selenium.Alert to handle the alerts in Selenium.
To get a handle to the open alert:
Alert alert = driver.switchTo().alert();
To Click on OK button:
alert.accept(); 
To click on Cancel button.
alert.dismiss() 
To get the text which is present on the Alert.
alert.getText(); 
To enter the text into the alert box
alert.sendkeys(String stringToSend); 
To Authenticate by passing the credentials
alert.authenticateUsing(Credentials credentials) 
Example 1: Alert with OK button

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AlertwithOKButton {

public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("http://demo.automationtesting.in/Alerts.html");

driver.findElement(By.xpath("//*[@id='OKTab']/button")).click(); // click
// on
// the
// button

Thread.sleep(5000);

driver.switchTo().alert().accept(); // this will close alert box by
// clicking OK button

}


}

Example 2: Alert with OK and Cancel buttons

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AlertwithOKAndCancelButtons {

public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("http://demo.automationtesting.in/Alerts.html");

driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[1]/ul/li[2]/a")).click();// button1

driver.findElement(By.xpath("//*[@id='CancelTab']/button")).click(); // button2

Thread.sleep(5000);

// close alert window by pressing OK button - first time
driver.switchTo().alert().accept();

driver.findElement(By.xpath("//*[@id='CancelTab']/button")).click(); // button2
Thread.sleep(5000);

// close alert window by pressing Cacel button - second time time
driver.switchTo().alert().dismiss();

}


}

Example 3: Alert with input box

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AlertwithInputbox {

public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("http://demo.automationtesting.in/Alerts.html");

driver.findElement(By.xpath("/html/body/div[1]/div/div/div/div[1]/ul/li[3]/a")).click();// button1

driver.findElement(By.xpath("//*[@id='Textbox']/button")).click(); // button2

Thread.sleep(5000);

driver.switchTo().alert().sendKeys("welcome");

Thread.sleep(5000);

driver.switchTo().alert().accept();

}


}

Followers