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());
}
}
- ‹ Working with Frames in Selenium Webdriver
- Defining Firefox Profile preferences using Selenium Webdriver ›
Comments
query
Hi ,
how to handle runtime pop up in selenium webdriver ?
Please help .
accpet() / dismiss()
How the selenium can know that accept() is for OK and dismiss() is for cancel buttons on alert? What if OK and Cancel buttons on alert got interchanged, do we need to use ROBOT class (Keyboard Events) for this scenario? Could you please explain this?
alert.authenticateUsing(Credentials credentials) is not valid
Please replace alert.authenticateUsing(Credentials credentials) with alert.authenticateUsing(new UserAndPassword("user","password")) as Credentials is Marker Interface and UserAndPassword class implements this interface.
Hi,
Hi,
selenium Easy is a very good site to learn automation, But one thing for Beginners it is very difficult to where start and end, please it is my advice to make it as step by step. tnx in advnce.
Thanks for your feedback.
We are working on making it more user friendly for a beginner on how to start and from where to start, Soon you will see some new changes.
Thank you for taking your time. I appreciate you.
Facing issue while accessing the URL of newly opened customized
I have login home page --> I log in into it and after login --> It opens new customized window pop up on which i do some activities like i can add module on the pop up page or i can do some other activities on the customized pop up window --> So here i am not able to get the access of this customized pop up window. So i need selenium code to get the access of the customized window in any language(like Java)
alert in webdriver
hi
i want to click on alert button but i don't have the xpath and ID and all.
how to click on to the alert button.
Add new comment