Articles → SELENIUM → Handling JavaScript alerts in Selenium

Handling JavaScript alerts in Selenium






Alert class in Java















Simple 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);
	}
}











Click to Enlarge


Confirm 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>
        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);
	}
}













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);
	}
}













Click to Enlarge


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);
	}
}









Click to Enlarge






Click to Enlarge


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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250