How to handle alerts in Selenium WebDriver

How to handle Alerts in Selenium WebDriver?

What we have learned so far?

Part 1 – What is Selenium WebDriver? How to setup Selenium?


Part 2 – Locators in Selenium WebDriver – Id, Name, Class Name, Tag Name, CSS, XPath

https://www.onlyfullstack.com/locators-in-selenium-webdriver/

Part 3 – Selenium WebDriver Commands

What is Alert?

Alert is a popup message which displays the notification to the user. Alert can also be used to take the confirmation from the user or to take some input from the user.

Types of Alert

1. Simple Alert
The alert() method displays an alert box with a specified message and an OK button. An alert box is often used if you want to make sure information comes through to the user.

simple alert selenium tutorial onlyfullstack

2. Confirmation Alert
The confirm() method displays a dialog box with a specified message, along with an OK and a Cancel button.

confirm alert selenium tutorial onlyfullstack

3. Prompt Alert
The prompt() method displays a dialog box that prompts the visitor for input. A prompt box is often used if you want the user to input a value before entering a page.

prompt alert selenium tutorial onlyfullstack

How to handle Alerts in Selenium WebDriver?

Alert interface provides the below methods to manipulate the alerts in Selenium Webdriver.
1. void accept()
This method is used to click on the ‘OK’ button of the alert.

webDriver.findElement(By.linkText("Simple Alert")).click();
Thread.sleep(1000); // wait for 1 sec to see the alert
webDriver.switchTo().alert().accept();

2. void dismiss()
This method is used to click on the ‘Cancel’ button of the alert.

webDriver.switchTo().alert().dismiss();

3. String getText()
This method is used to take the alert message.

webDriver.findElement(By.linkText("Simple Alert")).click();
Thread.sleep(1000); // wait for 1 sec to see the alert
String alertText = webDriver.switchTo().alert().getText();
System.out.println("Alert text = " + alertText);
webDriver.switchTo().alert().accept();

4. void sendKeys(String stringToSend)
This method is used to send some data to alert box.

webDriver.findElement(By.linkText("Prompt Alert")).click();
Thread.sleep(1000); // wait for 1 sec to see the alert
webDriver.switchTo().alert().sendKeys("Sample Text");
webDriver.switchTo().alert().accept();

Complete Program –

package com.onlyfullstack.selenium.alerts;

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

public class AlertOperations {

    public static void main(String[] args) {
        System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "chrome-web-driver/chromedriver");
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://onlyfullstack.blogspot.com/2020/03/best-demo-website-to-practice-selenium.html#alert-tutorial");

        webDriver.findElement(By.linkText("Simple Alert")).click();
        Alert alert = webDriver.switchTo().alert();
        String alertText = alert.getText();
        System.out.println("Alert text = " + alertText);
        alert.accept();

        webDriver.findElement(By.xpath("//*[@id="alert-tutorial"]/tbody/tr[3]/td/a")).click();
        Alert confirmAlert = webDriver.switchTo().alert();
        String confirmAlertText = confirmAlert.getText();
        System.out.println("Confirm Alert text = " + confirmAlertText);
        confirmAlert.dismiss();

        webDriver.findElement(By.linkText("Prompt Alert")).click();
        Alert promptAlert = webDriver.switchTo().alert();
        String promptAlertText = promptAlert.getText();
        System.out.println("Prompt Alert text = " + promptAlertText);
        promptAlert.accept();
    }

}

Output-

Alert text = Welcome to Only Fullstack Selenium Tutorial
Confirm Alert text = Do you want to continue?
Prompt Alert text = Enter your name

Source Code
You can find the complete source code on below GitHub repository – 
https://github.com/onlyfullstack/selenium-tutorial

Selenium Tutorial
https://www.onlyfullstack.com/selenium-webdriver-tutorial/