Selenium WebDriver – Handling Javascript Popups

In the latest release of Selenium 2.0 WebDriver Alert class is implemented.  Get the latest release of Selenium 2.0 from this Download link.  Download and unzip into \Libraries folder.  Add “selenium-server-standalone-2.0rc3” to the classpath (Add the External Jar to buildpath of the project’s properties in Eclipse IDE).

Here, I have taken an example HTML file which produces an alert box after clicking a button on the page.  Below is the code for that HTML file.  Copy and paste this HTML code into notepad and save it as “jscriptpopup.htm”

<script type="text/javascript">
function show_alert()
alert("This is an JavaScript alert box!");

<input type="button" onclick="show_alert()" value="Show alert box" />


Selenium 2.0 WebDriver provides class – Alert to handle the popups.  We can cancel the popup or else click on the OK button on the alert box.  We can also get the message on the alert box.

Before using the methods, alert class should be declared as follows:

Alert alert = driver.switchTo().alert();

Alert class provides various methods such as:

  1. accept()
  2. dismiss()
  3. getText()

This makes the Automation Tester’s life easier when there are lot of JavaScript popup’s in the Application under Test.

Create a new Java Class copy and paste the below code and then execute:

package com.selftechy.wdriver;
 * Author - Seetaram Hegde
import org.openqa.selenium.By;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class JscriptPopMain {
	public static void main(String[] args) {
		WebDriver driver=new FirefoxDriver();
		driver.findElement(By.xpath("//input[@value='Show alert box']")).click();
		Alert alert = driver.switchTo().alert();

Execution of the above code should produce the result as – “This is an JavaScript alert box!

Comments 5

  • I tried using javascript but it does not work on Chrome browser but it works on Firefox. I used
    Alert.accept() for chrome but it did not work so i used robot class to handle the pop ups. Can anyone tell me that on what browsers and versions, javascript could be executed.

  • Hi,

    I am getting error when i try to run the above code.
    org.openqa.selenium.UnhandledAlertException: Modal dialog present: This is an JavaScript alert box!
    Build info: version: ‘2.35.0’, revision: ‘c916b9d’, time: ‘2013-08-12 15:42:01’
    System info: ‘Linux’, os.arch: ‘amd64’, os.version: ‘3.9.0-030900-generic’, java.version: ‘1.6.0_45’

    Browser: FF 22
    OS: Ubuntu
    Selenium jar : 2.35
    Java : 1.6

  • This post is indeed very educative on handling authentication popups. However, I wish to automate the PKI login popup that comes in Firefox and asks for a security PIN. Is it possible using the method underlying in this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.