How can I press the alert ok button programmatically?

随声附和 提交于 2019-11-29 15:32:42

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:

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 catch 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.

ThiefMaster

You can't. Unless you use something that can control the browser (e.g. selenium).

If you do use selenium, have a look at Click in OK button inside an Alert (Selenium IDE)

If you can simulate a keypress of the space bar or enter key, then that will dismiss the alert. However you'd better be doing this from outside whatever makes the alert show up in the first place, since they tend to be blocking.

If this is JavaScript, you may be better off using console.log().

Using chooseOkOnNextConfirmation you can do that.

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);

For more information, go through this link link

selenium.chooseOkOnNextConfirmation(); is working for me in Selenium RC.

We have to comment the code for Alert OK button then it will work.

$this->chooseOkOnNextConfirmation();
$this->click('locator');
$this->getConfirmation();

The above process worked for me using Selenium RC with PHPUnit

Swift 3

you want to try this code in show alert and ok and cancel button

let sharephotoAction = UIAlertController.init(title: "Confirm Ticket", message:"Please Collect Your Ticket Before 1 Hours Ago in Location", preferredStyle: .alert )
        sharephotoAction.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (alertAction) in

            _ = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.Save), userInfo: nil, repeats: false)

        }))
        sharephotoAction.addAction(UIAlertAction(title: "Cancle", style: .default, handler:nil))

        self.present(sharephotoAction, animated: true, completion:nil)

You can use GSEvent.h for handling any type of keypress events, It is availble in GraphicsServices framework, It is private framewrk(so, you are not able submit it on appstore).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!