How to handle Alerts in Selenium WebDriver

HOME

driver.switchTo().alert()

Below is a perfect example that shows how to handle Simple Alert with Selenium

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.time.Duration;

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

        ChromeOptions options = new ChromeOptions();
        options.setImplicitWaitTimeout(Duration.ofSeconds(10));
        options.addArguments("start-maximized");
        ChromeDriver driver = new ChromeDriver(options);
        driver.navigate().to("https://the-internet.herokuapp.com/javascript_alerts");

        //Click on button to open Confirmation Box
        driver.findElement(By.xpath("//button[@onclick='jsAlert()']")).click();


        // accepting javascript alert
        Alert simpleAlert = driver.switchTo().alert();
        String alertText = simpleAlert.getText();

        //Print the message mentioned on the AlertBox
        System.out.println("Alert text is :" + alertText);

        //This step is only for demonstration purpose to show the alert box
        Thread.sleep(2000);
        simpleAlert.accept();

        //Close the current page
        driver.quit();
    }
}

2) Confirmation Alert

This alert comes with an option to accept or dismiss the alert. To accept the alert we can use Alert.accept() and to dismiss we can use the Alert.dismiss()

Below is an example that shows how to handle Confirmation Alert with Selenium.

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;

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

        ChromeOptions options = new ChromeOptions();
        options.setImplicitWaitTimeout(Duration.ofSeconds(10));
        options.addArguments("start-maximized");
        ChromeDriver driver = new ChromeDriver(options);
        driver.navigate().to("https://the-internet.herokuapp.com/javascript_alerts");

        //Click on button to open Confirmation Box
        driver.findElement(By.xpath("//button[@onclick='jsConfirm()']")).click();

        // accepting javascript alert
        Alert ConfirmationAlert = driver.switchTo().alert();
        String alertText = ConfirmationAlert.getText();
        System.out.println("Alert text is :" + alertText);
        ConfirmationAlert.accept();

        //Close the page
        driver.quit();
    }
}

3) Prompt Alert

In the prompt alert, we get an option to add text to the alert box. This is used when some input is required from the user. We will use the sendKeys() method to type something in the Prompt alert box. 

Below is an example that illustrates how to handle Prompt Alert using Selenium WebDriver.

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

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

        ChromeOptions options = new ChromeOptions();
        options.setImplicitWaitTimeout(Duration.ofSeconds(10));
        options.addArguments("start-maximized");
        ChromeDriver driver = new ChromeDriver(options);
        driver.navigate().to("https://the-internet.herokuapp.com/javascript_alerts");

        //Click on button to open Prompt Box
        driver.findElement(By.xpath("//button[@onclick='jsPrompt()']")).click();
        Alert promptAlert = driver.switchTo().alert();

        //Enter message in Alert Box
        promptAlert.sendKeys("Welcome to Selenium 4");
        promptAlert.accept();

        //This sleep is not necessary, just for demonstration
        Thread.sleep(2000);

        System.out.println("Prompt Alert text is :" + driver.findElement(By.id("result")).getText());

        //Close the current page
        driver.quit();
    }

}

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Leave a comment