Handling Alert in Selenium using Java

Alert are the java script pop up message box which ask for the permission or some input from the user. Some times they used to give warnings before proceeding to some tasks.

Selenium provides the interface and methods to handle the alert –

Interface ->   import org.openqa.selenium.Alert


Methods to handle Alert in selenium

1) accept() – to accept the alert ( click on “OK” button )
2) dismiss() –  to dismiss the alert ( click on “Cancel” button )
3) gettext() – to fetch the text from the alert
4) sendkeys() – to write text to the alert box


Different types of Alert in Selenium

There are 3 types of Alert

Handling Simple Alert in Selenium

Simple alert is used to display mainly the message to the user with “OK” button only.

 

Below Button to display the Simple Alert Alerts in selenium

Simple Alert Box With OK Button

Alerts in selenium

Java Code to handle simple alert

Description – In this program we will click on the button to display the alert box and then switch to alert and then click on “ok” button of alert box with accept() method.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Alert_Tutorial {

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

		// setting the gecko driver path
		System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
		 //openbrowser
		WebDriver driver=new FirefoxDriver();
		// implicit wait is defined with max waiting time of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		 //open url
		driver.get("https://padhle.com/automation-practice-demo/");
		// click on button to display alert
		driver.findElement(By.id("simplealert")).click();
// first we need to switch to alert to handle them		
// switch to alert
		Alert alt = driver.switchTo().alert();
		// thread sleep to see the the alert box for demo
		Thread.sleep(2000);
		// using accept method to press ok
		alt.accept();
		// close the browser
		driver.close();
	}

}

Handling Confirm Alert in Selenium

Confirm alert is used to display the prompt up box to confirm for a certain action like suppose user is deleting some data from its profile , a confirm prompt up box appears with “OK” and “Cancel” button.

Below Button to display the Confirm Alert

Alerts in seleniumConfirm Alert Box With OK and Cancel Button

Alerts in selenium

 

Java Code to handle confirm alert

Description – In this program we will click on the button to display the confirm box and then switch to alert and then click on “Ok” button of alert box with accept() method and click on “Cancel” button with dismiss() method.

package com.test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Alert_Tutorial {

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

		// setting the gecko driver path
		System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
		 //openbrowser
		WebDriver driver=new FirefoxDriver();
		// implicit wait is defined with max waiting time of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		 //open url
		driver.get("https://padhle.com/automation-practice-demo/");
		// click on button to display alert
		driver.findElement(By.id("confirmalert")).click();
		// switch to alert and accept
		Alert alt = driver.switchTo().alert();
		// thread sleep to see the the alert box for demo
		Thread.sleep(2000);
		// using accept method to press ok
		alt.accept();		
		
		Thread.sleep(2000);
		// ----- dismiss method - click on "Cancel button "
		// click on button to display alert
		driver.findElement(By.id("confirmalert")).click();
		// switch to alert and accept
		alt = driver.switchTo().alert();
		// thread sleep to see the the alert box for demo
		Thread.sleep(2000);
		// using accept method to press ok
		alt.dismiss();
		
		Thread.sleep(2000);
		// close the browser
		driver.close();
	}

}

Handling Prompt Alert in Selenium

Prompt alert is used to display the prompt up box to enter some text for a certain action like suppose user is entering some data to its profile via prompt up box , a confirm prompt up box appears with “OK” and “Cancel” button and text box.

Below Button to display the Prompt Alert

Alerts in selenium

Confirm Alert Box With OK and Cancel Button and TextAlert in selenium

Java Code to handle Prompt alert

Description – In this program we will click on the button to display the prompt alert box and then switch to alert and then enter the text with sendKeys() method and click on “Ok” button of alert box with accept() method and click on “Cancel” button with dismiss() method.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Alert_Tutorials {

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

		// setting the gecko driver path
		System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
		 //openbrowser
		WebDriver driver=new FirefoxDriver();
		// implicit wait is defined with max waiting time of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		 //open url
		driver.get("https://padhle.com/automation-practice-demo/");
		// click on button to display prompt alert
		driver.findElement(By.id("promptalert")).click();
		// switch to alert
		Alert alt = driver.switchTo().alert();
		// thread sleep to see the the alert box for demo
		Thread.sleep(2000);
		// get the text from alert
		alt.getText(); //-output "Please enter your name:"
		
		// send the data to padhle.com
		alt.sendKeys("padhle.com");
		// using accept method to press ok
		alt.accept();		
		
		Thread.sleep(2000);
		// ----- dismiss method - click on "Cancel button "
		// click on button to display alert
		driver.findElement(By.id("promptalert")).click();
		// switch to alert and accept
		alt = driver.switchTo().alert();
		// thread sleep to see the the alert box for demo
		Thread.sleep(2000);
		// using accept method to press ok
		alt.dismiss();
		
		Thread.sleep(2000);
		// close the browser
		driver.close();
	}
}

	}
}

This is all about the handling alerts in selenium , This article covers all the questions from the interview perspective.If you have any question please leave in comments or mail us.

Enroll for the Selenium online Training