How to handle javascript alerts, confirmation and prompts?

Generally JavaScript popups are generated by web application and hence they can be easily controlled by the browser.

Webdriver offers the ability to cope with javascript alerts using Alerts APIClick here to view Alert API Details

// Get a handle to the open alert, prompt or confirmation
Alert alert = driver.switchTo().alert();

Alert is an interface. There below are the methods that are used

//Will Click on OK button.
alert.accept();

// Will click on Cancel button.
alert.dismiss()

//will get the text which is present on th Alert.
alert.getText();

//Will pass the text to the prompt popup
alert.sendkeys();

//Is used to Authenticate by passing the credentials
alert.authenticateUsing(Credentials credentials)

Working with Alerts using Selenium Webdriver:

The below is the sample code for alerts, please copy and make an html file and pass it to the webdriver.

<html>
<head>
<title>Selenium Easy Alerts Sample </title>
</head>
<body>
<h2>Alert Box Example</h2>
<fieldset>
<legend>Alert Box</legend><p>Click the button to display an alert box.</p>
<button onclick="alertFunction()">Click on me</button>
<script>
function alertFunction()
{
alert("I am an example for alert box!");
}
</script>
</fieldset>
</body>
</html>

The below program will demonstrate you working on Alerts popup using above html file.

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class PopupsHandling {
	WebDriver driver=new FirefoxDriver();
	@Test
	public void ExampleForAlert() throws InterruptedException
	{
		driver.manage().window().maximize();
		driver.get("file:///C:/path/alerts.html");
		Thread.sleep(2000);
		driver.findElement(By.xpath("//button[@onclick='alertFunction()']")).click();
		Alert alert=driver.switchTo().alert();
		System.out.println(alert.getText());
		alert.accept();
	}
}

Working with Confirmation Popups

The below is the sample code for confirmation Popup, please copy and make an html file and pass it to the webdriver as below.

<html>
<head>
<title>Selenium Easy Confirm popup Sample </title>
</head>
<body>
<h2>Confirm Box Example</h2>
<fieldset>
<legend>Confirm Box</legend>
<p>Click the button to display a confirm box.</p>
<button onclick="confirmFunction()">Click on me</button>
<p id="confirmdemo"></p>
<script>
function confirmFunction()
{
var cb;
var c=confirm("I am an Example for Confirm Box.\n Press any button!");
if (c==true)
  {
  cb="You Clicked on OK!";
  }
else
  {
  cb="You Clicked on Cancel!";
  }
document.getElementById("confirmdemo").innerHTML=cb;
}
</script>
</fieldset>
</body>
</html>

The below program will demonstrate you working on Confirmation popup using above html file.

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class PopupsHandling {
	WebDriver driver=new FirefoxDriver();
	@Test
	public void ExampleForConfirmBox() throws InterruptedException
	{
		driver.manage().window().maximize();
		driver.get("file:///C:/path/confirmation.html");
		Thread.sleep(2000);
		driver.findElement(By.xpath("//button[@onclick='confirmFunction()']")).click();
		Alert alert=driver.switchTo().alert();
		System.out.println(alert.getText());
		alert.dismiss();
	}
}

Working with Prompt Popups.

In prompt, you can enter the text using webdriver sendkeys("text..")

The below is the sample code for prompt popup, please copy and make an html file and pass it to the webdriver as below.

<html>
<head>
<title>Selenium Easy Prompt popup Sample </title>
</head>
<body>
<h2>Prompt Box Example</h2>
<fieldset>
<legend>Prompt Box</legend>
<p>Click the button to demonstrate the prompt box.</p>
<button onclick="promptFunction()">Click on me</button>
<p id="promptdemo"></p>
<script>
function promptFunction()
{
var x;
var person=prompt("Please enter your name","Your name");
if (person!=null)
  {
  x="Hello " + person + "! Welcome to Selenium Easy..";
  document.getElementById("promptdemo").innerHTML=x;
  }
}
</script>
</fieldset>
</body>
</html>

The below program will demonstrate you working on prompt popup using above html file.

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class PopupsHandling {
	WebDriver driver=new FirefoxDriver();
	
	@Test
	public void ExampleForPromptBox() throws InterruptedException
	{
		driver.manage().window().maximize();
		driver.get("file:///C:/path/prompt.html");
		Thread.sleep(2000);
		driver.findElement(By.xpath("//button[@onclick='promptFunction()']")).click();
		Alert alert=driver.switchTo().alert();
		driver.switchTo().alert().sendKeys("Helllo");
		alert.accept();
		System.out.println(alert.getText());
	}
}
Selenium Tutorials: 

Comments

Thanks for providing good article on "How to handle javascript alerts, confirmation and prompts".

java script in used prompt exampal releted books and marks????

Great work... I am new to Selenium, but you helped me at a great extent to continue with the Selenium webdriver.

The selenium have randomly bug when accept allert - "org.openqa.selenium.WebDriverException: a.document.getElementsByTagName(...)[0].getButton is not a function", how i can work around this ?

hi,
I want to send two keys (1.username, 2.password).
please suggest how can I achieve this?

Thanks for share this

how to write syntax for handling pop up

I think this solution didn't work in google chrome. Did you know any alternative for chrome?

Great article. But can you explain how can we get the text displayed 'Your name' from the popup prompt alert

In a Registration form, mobile number is unique field, it means it does not accept duplicate mobile number, how can i handle this in selenium ?

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.