How can I press the OK button in a JS alert programmatically?
What I want to do: every time after the alert is created, the OK button is pressed.
This is for a UI test using Selenium RC.
Also, I have already checked: Click in OK button inside an Alert (Selenium IDE).
Edit: I had already used chooseOkOnNextConfirmation() and placed it before clicking the button the generated the alert. I also tried placing it after. Nothing worked!
If you can actually see an alert dialog, then it can’t be done. Selenium should handle it for you. But, as stated in Selenium documentation:
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 catch
onloadJS alerts, you need to use WebDriver alert handling.That said, you can use
Robotorselenium.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 thealertmessage, however.Robothas all the useful keys mapped to constants, so that will be easy. WithkeyPressNative(), you want to use10as value for pressing Enter or27for Esc since it works with ASCII codes.