Articles → SELENIUM → Handling Javascript Alerts In Selenium
Handling Javascript Alerts In Selenium
Alert Class In Java
- accept() – Method used to click ‘Ok’ button in an alert.
- dismiss() – Method used to click ‘Cancel’ button in an alert
- getText() – Method used to get text from an alert.
- sendKeys() – Method used to send text to an alert.
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);
}
}
- An alert message appears
- ‘Ok’ button is clicked and alert message disappears.
- An alert message will be displayed in console.
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);
}
}
- An alert message appears
- ‘Ok’ button is clicked and alert message disappears.
- An alert message text will be displayed in console.
- ‘true’ is written on HTML page.
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);
}
}
- An alert message appears
- ‘Cancel’ button is clicked and alert message disappears.
- An alert message text will be displayed in console.
- ‘false’ is written on HTML page.
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);
}
}
- A prompt alert message will appear with ‘18’ as its default value
- Value will be changed to ‘19’.
Click to Enlarge
- ‘Cancel’ button is clicked and alert message disappears.
- An alert message text will be displayed in console.
Click to Enlarge