73

I need to click the 'Ok' button inside an alert window with a Selenium command. I've tried assertAlert or verifyAlert but they don't do what I want.

It's possible the click the 'Ok' button? If so, can someone provide me an example of the Selenium IDE command?

14 Answers 14

54
+25

Try Selenium 2.0b1. It has different core than the first version. It should support popup dialogs according to documentation:

Popup Dialogs

Starting with Selenium 2.0 beta 1, there is built in support for handling popup dialog boxes. After you’ve triggered and action that would open a popup, you can access the alert with the following:

Java

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

Ruby

driver.switch_to.alert

This will return the currently open alert object. With this object you can now accept, dismiss, read it’s contents or even type into a prompt. This interface works equally well on alerts, confirms, prompts. Refer to the JavaDocs for more information.

3
  • 4
    is there any way doing from selenium ide? Feb 15, 2014 at 12:23
  • 4
    This answer doesn't apply to IDE which was asked in the post.
    – DMart
    Apr 29, 2016 at 18:34
  • the title is Selenium IDE
    – 123
    Mar 21, 2021 at 20:36
47

To click the "ok" button in an alert box:

driver.switchTo().alert().accept();
10

This is an answer from 2012, the question if from 2009, but people still look at it and there's only one correct (use WebDriver) and one almost useful (but not good enough) answer.


If you're using Selenium RC and can actually see an alert dialog, then it can't be done. Selenium should handle it for you. But, as stated in Selenium documentation:

Selenium tries to conceal those dialogs from you (by replacing window.alert, window.confirm and window.prompt) so they won’t stop the execution of your page. If you’re seeing an alert pop-up, it’s probably because it fired during the page load process, which is usually too early for us to protect the page.

It is a known limitation of Selenium RC (and, therefore, Selenium IDE, too) and one of the reasons why Selenium 2 (WebDriver) was developed. If you want to handle onload JS alerts, you need to use WebDriver alert handling.

That said, you can use Robot or selenium.keyPressNative() to fill in any text and press Enter and confirm the dialog blindly. It's not the cleanest way, but it could work. You won't be able to get the alert message, however.

Robot has all the useful keys mapped to constants, so that will be easy. With keyPressNative(), you want to use 10 as value for pressing Enter or 27 for Esc since it works with ASCII codes.

4

1| Print Alert popup text and close -I

Alert alert = driver.switchTo().alert();
System.out.println(closeAlertAndGetItsText());

2| Print Alert popup text and close -II

Alert alert = driver.switchTo().alert();
System.out.println(alert.getText()); //Print Alert popup
alert.accept(); //Close Alert popup

3| Assert Alert popup text and close

Alert alert = driver.switchTo().alert();
assertEquals("Expected Value", closeAlertAndGetItsText());
4

If you using selenium IDE then you have to click on Ok button manually because when alert message command run that time browser stop working and if you want to click on ok button automatically then you have to use selenium RC or webdriver and below command is for Selenium IDE

In selenium ide use storeeval command, different type of boxes

    storeEval | alert("This is alert box")                           |
    storeEval | prompt("This is prompt box. Please enter the value") | text
    storeEval | confirm("this is cofirm box")   |
2

You might look into chooseOkOnNextConfirmation, although that should probably be the default behavior if I read the docs correctly.

0
2

The question isn't clear - is this for an alert on page load? You shouldn't see any alert dialogues when using Selenium, as it replaces alert() with its own version which just captures the message given for verification.

Selenium doesn't support alert() on page load, as it needs to patch the function in the window under test with its own version.

If you can't get rid of onload alerts from the application under test, you should look into using GUI automation to click the popups which are generated, e.g. AutoIT if you're on Windows.

3
  • AutoIT won't work across all browsers, because for example for Opera (or AFAIK new Firefox), Alert box is not a system window/control, but rather a custom graphical element rendered by the browser, so AutoIt is not able to detect or click it. Feb 8, 2011 at 11:49
  • true that onload alerts are not possible to reach. Have the same with security AutoAuth warning, but on Linux.
    – Dee
    Aug 4, 2015 at 8:54
  • The OP specified Selenium IDE and not just Selenium
    – icc97
    Sep 21, 2015 at 12:05
2

Use the Alert Interface, First switchTo() to alert and then either use accept() to click on OK or use dismiss() to CANCEL it

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

or

Alert alert_box = driver.switchTo().alert();
alert_box.dismiss(); 
2

about Selenium IDE, I am not an expert but you have to add the line "choose ok on next confirmation" before the event which trigger the alert/confirm dialog box as you can see into this screenshot:

Selenium IDE order matter

2
  • 1
    In theory, yes, but in practice Selenium IDE 3.17 is still having problem handling Chrome alert on the Windows 10 platform.
    – Johnny Wu
    Feb 10, 2021 at 20:44
  • I try with Chrome/Chromium (version 90.0.4402.0 on Windows 10) and the solution recorded by Selenium IDE 3.17 is the following list: - answer on next prompt - assert prompt - webdriver answer on visible prompt - click @al-sargent mention is correct Feb 23, 2021 at 9:45
0

assertAlert ought to do the trick. I see in the docs that alerts generated in a page's OnLoad event handler cannot be scripted this way (and have experienced it myself, alas, due to the ASP.NET page lifecycle). Could that be what you're running into?

0
0

For selenium, an alert is the one which raised using javascript e.g.

 javascript:alert();

There is one basic check to verify whether your alert is actually a javascript alert or just a div-based box for displaying some message. If its a javascript alert, you wont be able to see it on screen while running the selenium script.

If you are able to see it, then you need to get the locator of the ok button of the alert and use selenium.click(locator) to dismiss the alert. Can help you better if you can provide more context:

  1. IDE or RC?
  2. HTML code of the alert
  3. your selenium script.

Vamyip

0

Use chooseOkOnNextConfirmation() to dismiss the alert and getAlert() to verify that it has been shown (and optionally grab its text for verification).

selenium.chooseOkOnNextConfirmation();  // prepares Selenium to handle next alert
selenium.click(locator);
String alertText = selenium.getAlert(); // verifies that alert was shown
assertEquals("This is a popup window", alertText);
...
0
0

This is Pythoncode
Problem with alert boxes (especially sweet-alerts is that they have a delay and Selenium is pretty much too fast)

An Option that worked for me is:

while True:
    try:
        driver.find_element_by_xpath('//div[@class="sweet-alert showSweetAlert visible"]')
        break
    except:
        wait = WebDriverWait(driver, 1000)

confirm_button = driver.find_element_by_xpath('//button[@class="confirm"]')
confirm_button.click()
0

The new Selenium IDE (released in 2019) has a much broader API and new documentation.

I believe this is the command you'll want to try:

webdriver choose ok on visible confirmation

Described at:

https://www.seleniumhq.org/selenium-ide/docs/en/api/commands/#webdriver-choose-ok-on-visible-confirmation

There are other alert-related API calls; just search that page for alert

1
  • this still doesn't seem to work on Chrome in Windows.
    – Johnny Wu
    Feb 10, 2021 at 20:43