5
votes

Using selenium2library to automate my web application with robot framework. I am not able to accept a browser alert using the existing keywords.

Getting the below exception while trying to accept the alert.

UnexpectedAlertPresentException: Alert Text: Delete selected trusted provider(s)?
<super: <class 'WebDriverException'>, <UnexpectedAlertPresentException object>>

have tried the below selenium2library keywords Alert Should Be Present, Get Alert Message , Confirm Action, etc.

Please guide me on how to proceed.

2

2 Answers

7
votes

At last I have found a way to handle alerts in Robot Framework.

We just have to use "Choose Ok On Next Confirmation" with "Confirm Action" Keyword.

Say if I want to accept an Alert prompting for Yes or No for deleting an resource using selenium you can use the below code.

Choose Ok On Next Confirmation      
Click Element   //a[contains(.,'Delete')]
Confirm Action  

This will be accepting the Alert. If you want to dismiss or deny the alert you can simply use the keyword Alert Should Be Present which will auto dismiss the alert.

2
votes

As of Selenium2Library 3.0 you can use the Handle Alert keyword with the actions:

  • ACCEPT: Accept the alert i.e. press Ok. Default.
  • DISMISS: Dismiss the alert i.e. press Cancel.
  • LEAVE: Leave the alert open.

There is also an optional 2nd argument that you can use to supply a timeout. It is also possible to store the text from the alert in a variable.

Handle Alert Keyword - Selenium2Library Docs http://robotframework.org/Selenium2Library/Selenium2Library.html#Handle%20Alert