如何以编程方式按警报确定按钮? [英] How can I press the alert ok button programmatically?

查看:89
本文介绍了如何以编程方式按警报确定按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式在JS警报中按下OK按钮?

How can I press the OK button in a JS alert programmatically?

我要做什么:每次创建警报后,都按下OK按钮

What I want to do: every time after the alert is created, the OK button is pressed.

这是使用Selenium RC进行的UI测试。

This is for a UI test using Selenium RC.

此外,我已经检查了:在Alert(Selenium IDE)中单击确定按钮

Also, I have already checked: Click in OK button inside an Alert (Selenium IDE).

编辑:我已经使用 chooseOkOnNextConfirmation()并将其放置在单击生成警报的按钮之前。我也尝试过放置它。没事!

I had already used chooseOkOnNextConfirmation() and placed it before clicking the button the generated the alert. I also tried placing it after. Nothing worked!

推荐答案

如果您实际上可以看到警报对话框,则无法完成。硒应该为您处理。但是,如Selenium文档中所述

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试图向您隐藏这些对话框(通过替换
window.alert,window.confirm和window.prompt),以便它们不会停止页面的
执行。如果您看到警告弹出窗口,则可能是
,可能是因为它是在页面加载过程中触发的,通常对于我们来说保护该页面还为时过早。

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.

这是Selenium RC(因此也包括Selenium IDE)的已知局限性,也是开发Selenium 2(WebDriver)的原因之一。如果要捕获 onload JS警报,则需要使用WebDriver 警报处理

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.

也就是说,您可以使用 机器人 selenium.keyPressNative() 填写任何文本,然后按 Enter 并盲目确认对话框。这不是最干净的方法,但是它可以工作。但是,您将无法获得警报消息。

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 将所有有用的键映射到常量,因此这很容易。使用 keyPressNative(),您要使用 10 作为按 Enter 27 用于 Esc ,因为它与 ASCII码

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.

这篇关于如何以编程方式按警报确定按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆