Handling JavaScript Alert in Selenium WebDrive

Handling JavaScript Alert in WebDriver is big question if you have just started learning WebDriver. Because we always get multiple pop-ups some time we get information about validity of input and some time these pop-up speaks about error, and also about response.

But as a tester we need to verify the alert message and for the same we need to handle the Alert pop up. So its time to celebrate because WebDriver have provided us with Alert class to deal with JavaScript Alert.

JavaScript Alert in WebDriverSince I have created this alert through a basic html file and  it has one button Ok. Butthere is another kind of alert known as Confirm Box Alert and it normally have two button

1-Ok button
2- Cancel button

Since both alert is handled in the same way so here I am taking the Confirm Box Alert here

Scenario : Suppose we have one button, and once we hit on the button a alert appears with two button Ok and Cancel
a) In first case if end user click on Ok button

b) In second case we want to click on Cancel button

So above code would remain same only thing that would change in above script is

For clicking on Cancel we need to use dismiss() method of Alert Class

Hope this script is fruitful for you and you would be Handling JavaScript Alert in WebDriver.


  • Pingback: Handling JavaScript Alert in WebDriver | Selenium Alchemy

  • the same piece of code is working for IE and FF but it is not working for chrome browser

    • can you elaborate your problem…As per my understanding such anomaly should not come, because I have already tested this code for Chrome browser along with other Browsers.
      So please send me the stacktrace so that i could understand your chrome specific problem..

      Thank you and Hope to listen from you soon.

      • I was trying to implement this on IRCTC site, soon after opening the application using chrome driver i simpliy clicked on login button without entering any uid, pswd so it is throwing an alert i was supposed to handle that alert using Webdriver. but when i am using the above code it is clicking on that button but after opening that alert the execution was not switched to the alert thats why my script execution was stopped there… and even in the log it is displaying unexpected alert open error message

      • and also the preoblem might be with my chrome browser version , mine is 26.0.1410.64 m

  • Hi,

    I was trying to implement this on IRCTC site, soon after opening the application using chrome driver i simpliy clicked on login button without entering any uid, pswd so it is throwing an alert i was supposed to handle that alert using Webdriver. but when i am using the above code it is clicking on that button but after opening that alert the execution was not switched to the alert thats why my script execution was stopped there…

    • import java.util.concurrent.TimeUnit;

      import org.junit.Before;
      import org.junit.Test;
      import org.openqa.selenium.By;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.chrome.ChromeDriver;

      public class IRCTC {
      public WebDriver driver;

      public void launch()
      System.setProperty(“webdriver.chrome.driver”,”E:DD MISHRAworkspacechromedriver_win_26.0.1383.0chromedriver.exe”);
      driver = new ChromeDriver();

      public void test()
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      System.out.println(“Finally it click on Alert “);



      This code is working for me and hoping that it will help u in resolving your query

      • Neeraj Srivastava

        Thanks for this but this code is not working for me, my Chrome version is 27.0.1453.110 m,

        Same issue I am facing, code got stuck at handle line, below is the err description:
        org.openqa.selenium.WebDriverException: unknown error: unhandled inspector error: {“code”:-32603,”message”:”No JavaScript dialog to handle”}
        (Session info: chrome=27.0.1453.110)
        (Driver info: chromedriver=0.8,platform=Windows NT 5.1 SP3 x86) (WARNING: The server did not provide any stacktrace information)
        Command duration or timeout: 16 milliseconds
        Build info: version: ‘2.32.0’, revision: ‘6c40c18’, time: ‘2013-04-09 17:23:22’
        System info: os.name: ‘Windows XP’, os.arch: ‘x86’, os.version: ‘5.1’, java.version: ‘1.7.0_25’
        Session ID: b58f6213098f1ddf35ed7afececd31d2
        Driver info: org.openqa.selenium.chrome.ChromeDriver
        Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, rotatable=false, driverVersion=0.8, locationContextEnabled=true, version=27.0.1453.110, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:187)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:569)
        at org.openqa.selenium.remote.RemoteWebDriver$RemoteAlert.accept(RemoteWebDriver.java:863)
        at TC_Home_Site.homeSite03(TC_Home_Site.java:469)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

        Can you help me in this, is there any other thing is require to do this before handle the code.

        Please let me know on this,


  • @Neeraj

    Please add some wait or sleep in your code before handling the pop up

  • Solved my problem..Nice Blog 🙂

  • Pingback: WebDriver’s most popular Commands | Abode QA

  • Pingback: Handling Multiple Windows in WebDriver | Abode QA

  • Pingback: Adding add-on in Firefox and Chrome using WebDriver | Abode QA

  • Pingback: Email your failing Selenium WebDriver scripts’ Stack Trace to your Email Id | Abode QA

  • Pingback: Unexpected error launching Internet Explorer. Protected Mode must be set to the same value | Abode QA

  • Hello,

    I have an issue with chrome driver . I have put wait statements but still the pop up is not getting recognized as alert. Confirm Navigation alert in chrome with text “Are you sure you want to leave this page”

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

  • Thanks for providing good article on “Handling JavaScript Alert in WebDriver”. http://reditblog.blogspot.in/

  • As I want to copy the text from popup window and I have to trim the text and pass the value as the input in the next screen – pls help me out.

  • I have to mention an alert testcase before going to click edit button, can you please let me know how to do that

Leave a Reply

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