Handle JavaScript alerts using Selenium

Types of JavaScript alerts :

There are 3 types of alerts- Alert box, Prompts and Confirm alerts. In Selenium, we handle them using the Alert class.

When there is an alert on a page, there can be no other actions performed, until the alert is accepted or rejected. So the driver loses its access to all other elements on the page. A JavaScript alerts are not part of the DOM. They are part of the browser. Driver will have to switch to the alert context in order to take any action on it.

switchTo() :

It is a method available in the WebDriver interface, specifically the TargetLocator inner interface in WebDriver. It helps to switch context i.e. alerts or frames or a different window.

Alert alert = driver.switchTo().alert(); //the driver will swicth to the alert context

Alert :

It is an interface in Selenium which helps to handle JavaScript alerts. It provides methods like accept(). dismiss(), getText(), sendKeys() etc. Let's see the below example:

public class AlertsHandle {
    public static void main(String[] args) {        
        WebDriver driver = new ChromeDriver();
        driver.get("https://the-internet.herokuapp.com/javascript_alerts");

        By jsAlert = By.xpath("//button[text()='Click for JS Alert']");
        By jsConfirm = By.xpath("//button[text()='Click for JS Confirm']");
        By jsPrompt = By.xpath("//button[text()='Click for JS Prompt']");

        driver.findElement(jsAlert).click();
        Alert alert = driver.switchTo().alert();// o/p : I am a JS Alert 
        System.out.println(alert.getText());
        alert.accept();        
    }
}

If we click on the "click for JS alert" we will see below alert. alert.accept() will simply accept the alert and driver will automatically switch its context to default.

Click on the "click for JS confirm" :

public class AlertsHandle {
    public static void main(String[] args) {        
        WebDriver driver = new ChromeDriver();
        driver.get("https://the-internet.herokuapp.com/javascript_alerts");

        By jsAlert = By.xpath("//button[text()='Click for JS Alert']");
        By jsConfirm = By.xpath("//button[text()='Click for JS Confirm']");
        By jsPrompt = By.xpath("//button[text()='Click for JS Prompt']");

        driver.findElement(jsConfirm).click();
        Alert alert = driver.switchTo().alert();
        System.out.println(alert.getText()); // o/p: I am a JS Confirm
        alert.dismiss();     
    }
}

The alert.dismiss() will click on Cancel button and close the alert.

Now, click on the "click for JS prompt" :

public class AlertsHandle {
    public static void main(String[] args) throws InterruptedException{        
        WebDriver driver = new ChromeDriver();
        driver.get("https://the-internet.herokuapp.com/javascript_alerts");

        By jsAlert = By.xpath("//button[text()='Click for JS Alert']");
        By jsConfirm = By.xpath("//button[text()='Click for JS Confirm']");
        By jsPrompt = By.xpath("//button[text()='Click for JS Prompt']");

        driver.findElement(jsPrompt).click();
        Alert alert = driver.switchTo().alert();
        alert.sendKeys("Hello");
        System.out.println(alert.getText()); //o/p: I am a JS prompt
        alert.accept();                
    }
}

In this case, if we write alert.dismiss() , the text entered in the prompt will be dismissed as well.