Articles → SELENIUM → Handling JavaScript alerts in Selenium

Handling JavaScript alerts in Selenium






Alert class in Java















Simple alert box




Picture showing the different parts of the alert box

Click to Enlarge





<!DOCTYPE html>
<html lang="en"
	xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
		<script>
	alert("This is the test alert message");
	</script>
	</body>
</html>




package com.FirstSeleniumProject;

import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class MyClass {

	public static void main(String[] args) {
		WebDriver driver;
		String service = "IEDriverServer path";
		System.setProperty("webdriver.ie.driver", service);
		driver = new InternetExplorerDriver();
		driver.get("http://localhost:18275/Page1.html");

		Alert alert = driver.switchTo().alert();
		String alertMessage = alert.getText();
		alert.accept();
		System.out.println(alertMessage);
	}
}










Picture showing how to handle simple alert box in selenium

Click to Enlarge


Confirm alert box




Picture showing the different parts of the confirm box

Click to Enlarge





<!DOCTYPE html>
<html lang="en"
	xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
		<script>
        var out = confirm("Are you sure you want to click on Ok");
        document.write(out);
    </script>
	</body>
</html>




package com.FirstSeleniumProject;

import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class MyClass {

	public static void main(String[] args) {
		WebDriver driver;
		String service = "IEDriverServer path";
		System.setProperty("webdriver.ie.driver", service);
		driver = new InternetExplorerDriver();
		driver.get("http://localhost:18275/Page1.html");

		Alert alert = driver.switchTo().alert();
		String alertMessage = alert.getText();
		alert.accept();
		System.out.println(alertMessage);
	}
}












Picture showing how the confirm box is handled using selenium when ok button is clicked

Click to Enlarge



package com.FirstSeleniumProject;

import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class MyClass {

	public static void main(String[] args) {
		WebDriver driver;
		String service = "IEDriverServer path";
		System.setProperty("webdriver.ie.driver", service);
		driver = new InternetExplorerDriver();
		driver.get("http://localhost:18275/Page1.html");

		Alert alert = driver.switchTo().alert();
		String alertMessage = alert.getText();
		alert.dismiss();
		System.out.println(alertMessage);
	}
}












Picture showing how the confirm box is handled using selenium when cancel button is clicked

Click to Enlarge


Prompt alert box




Picture showing the different components of the prompt alert box

Click to Enlarge



<!DOCTYPE html>
<html lang="en"
	xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
		<script>
       prompt("Please enter your age", "18");
    </script>
	</body>
</html>




package com.FirstSeleniumProject;

import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class MyClass {

	public static void main(String[] args) {
		WebDriver driver;
		String service = "IEDriverServer path";
		System.setProperty("webdriver.ie.driver", service);
		driver = new InternetExplorerDriver();
		driver.get("http://localhost:18275/Page1.html");

		Alert alert = driver.switchTo().alert();
		String alertMessage = alert.getText();
		alert.sendKeys("19");
		alert.dismiss();
		System.out.println(alertMessage);
	}
}








Picture showing the prompt alert box for the user to enter the value

Click to Enlarge





Picture showing how the prompt alert box is handled in selenium

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Saturday, April 15, 2017

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250