JavaScript Alerts and Selenium/Webdriver.

One of the most popular question among selenium/Webdriver users. I used that couple of places to handle.

Selenium RC: if you are expecting javaScript alert and  you want to handle using OK then use following commands and then you can use your code.

selenium.chooseCancelOnNextConfirmation()
selenium.chooseOkOnNextConfirmation()

Note: In Firefox 4.0 and higher version, you will see alerts like following and its impossible to handle this via selenium.

There is a workaround to handle this.

Type ‘about:config’ in Address bar of firefox browser and accept the warning.

Type “tab_”. You will see prompts.tab_model.enabled option with Value: true.

Double click on the row which will make it false.

Now alert will look like:

 

Webdriver:

There is command to handle alert are:

driver.SwitchTo().Alert().Accept();
driver.SwitchTo().Alert().Dismiss();
driver.SwitchTo().Window(“windowName”);

You can handle as per your need.

 

Leave a Reply