How to Handle Alerts in Selenium Webdriver?

Welcome back to the Selenium WebDriver Testing Free Training series. We are going to talk about how to manage alerts with Selenium web driver code.

What are the alerts?

Well, alerts are JavaScript’s functionalities. Alerts show text in the dialog box that pops up on the screen, and all browsers support the alerts.

Where alerts are used?

Before jumping to the main discussion, let’s take a look at where alerts are used, and what are the types of alerts.

Let’s say you are submitting a form, and it has a number of fields, in other words, the form is pretty long. Somehow the user missed one or more fields or entered the wrong value, that’s where the alert comes, and it shows the error.  Or let’s say the developer wants to alert the user that your session is about to end.

There will be more scenarios where developers can use alerts.

What are the types of alerts?

All right. Let’s move to the next point, the types of alerts.

1. Alert Box

This alert box is used when you wish to show some information to the user.

When an alert box pops up, the user will have to click “OK” to proceed.

Webdriver script to handle the Alert box

Let’s see code snippets to manage the alert box:
package seleniumWebDriverCodes;

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

public class HandleAlertTraining {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "chromedriverpath");
        driver = new ChromeDriver();
        driver.get("http://www.training.qaonlinetraining.com/testPage.php");
        // accept(You accepted the alert.)
        driver.findElement(By.id("alert")).click();
        Alert alert = driver.switchTo().alert();
        System.out.println(alert.getText());
        Thread.sleep(3000);
        alert.accept();

    }

}

Explanation Alert box code

In the above code, you can see switchTo() method. This method helps to switch the control to a new window or alert box from the main window.

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

The above code will switch control to the alert box. To switch back control to the main window, you can use the following code:

driver.switchTo().defaultContent()
driver.switchTo().alert().getText()

The above code snippet is used to get an alert message.

Watch Handle Simple Alert using Selenium webdriver

Read How to handle Iframes using Selenium Webdriver

2. Confirm Box

As the name suggests, this box is used when you want to user to verify or accept. When the confirmation box pops up, the user has to be clicked on the OK button to accept or click on the Cancel button to deny.

Webdriver script to handle the Confirm box

Let’s see code snippets to manage the confirm box:
package seleniumWebDriverCodes;

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

public class HandleAlertTraining {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "chromedriverpath");
        driver = new ChromeDriver();
        driver.get("http://www.training.qaonlinetraining.com/testPage.php");

        // dismiss(You pressed Cancel!)
        driver.findElement(By.id("confirm")).click();
        Alert alert1 = driver.switchTo().alert();
        System.out.println(alert1.getText());
        Thread.sleep(3000);
        alert1.dismiss();

    }

}

Explanation Confirm Alert box

If you see the above code snippets, there is one method:

driver.switchTo().alert().dismiss(): This is to cancel or deny.

Watch Handle Confirm Alert using Selenium webdriver

 3. Prompt Box

This box is used to take user input before entering a page. So, when a prompt box pops up, the user will have to click either “OK” or “Cancel” to proceed after entering an input value.

Webdriver script to handle the Prompt alert box

package seleniumWebDriverCodes;

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

public class handleAlertTraining {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "chromedriverpath");
        driver = new ChromeDriver();
        driver.get("http://www.training.qaonlinetraining.com/testPage.php");
        
        //accept and Type(Hello Mr. Bond! How are you today?)
        driver.findElement(By.id("prompt")).click();
        Alert alert2 = driver.switchTo().alert();
        System.out.println(alert2.getText());
        Thread.sleep(3000);
        alert2.sendKeys("Mr. Bond");
        alert2.accept();

    }

}

If you see the above code snippets, there is one method:

driver.switchTo().alert().accept(): This is to accept.

Watch Handle Prompt Alert using Selenium webdriver

Instructor-led Training

Important note: we like to tell you that free tutorials are useful to get started but if you are interested in the best online LIVE Master of Automation Testing training program from the experts, please refer to the following link:For Instructor-led training

Master of Functional Automation Testing

For Self-Driven training

Automation with Selenium WebDriver (Java)

Conclusion

This is how we manage the alerts thanks to selenium web driver code.  Similarly, a web page also has one or more than one iframe, but managing or handling the iframes is a bit different that we will see in the next tutorial.

Until then, Happy Learning 😀