单击警报内的“确定”按钮(Selenium IDE)
我需要使用 Selenium 命令单击警报窗口内的“确定”按钮。我尝试过 assertAlert
或 verifyAlert
但它们没有执行我想要的操作。
可以单击“确定”按钮吗?如果是这样,有人可以给我提供 Selenium IDE 命令的示例吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
尝试使用 Selenium 2.0b1。它的核心与第一个版本不同。根据 文档,它应该支持弹出对话框:
弹出对话框
从 Selenium 2.0 开始的 beta 1 中,内置了对处理弹出对话框的支持。触发将打开弹出窗口的操作后,您可以使用以下命令访问警报:
Java
Ruby
这将返回当前打开的警报对象。有了这个对象,您现在可以接受、关闭、阅读它的内容,甚至可以输入提示。该界面在警报、确认、提示方面同样有效。请参阅 JavaDocs 以获取更多信息。
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
Ruby
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.
要单击警告框中的“确定”按钮:
To click the "ok" button in an alert box:
这是 2012 年的一个答案,这个问题是 2009 年的,但人们仍然会看它,并且只有一个正确的(使用 WebDriver)和一个几乎有用(但不够好)的答案。
如果您使用 Selenium RC 并且实际上可以看到警告对话框,那么这是无法完成的。硒应该为你处理它。但是,如 Selenium 文档中所述:
这是 Selenium RC(因此也是 Selenium IDE)的一个已知限制,也是开发 Selenium 2 (WebDriver) 的原因之一。如果您想处理
onload
JS 警报,您需要 使用 WebDriver 警报处理。也就是说,您可以使用
Robot
或selenium.keyPressNative()
填写任何文本,然后按 Enter 并盲目确认对话框。这不是最干净的方法,但它可以工作。但是,您将无法收到警报
消息。Robot
将所有有用的键映射到常量,因此这很容易。使用keyPressNative()
,您希望使用10
作为按 Enter 的值,或使用27
作为 Esc 的值 因为它适用于 ASCII 代码。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:
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
orselenium.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 thealert
message, however.Robot
has all the useful keys mapped to constants, so that will be easy. WithkeyPressNative()
, you want to use10
as value for pressing Enter or27
for Esc since it works with ASCII codes.1|打印警报弹出文本并关闭 -I
2|打印警报弹出文本并关闭 -II
3|断言警报弹出文本并关闭
1| Print Alert popup text and close -I
2| Print Alert popup text and close -II
3| Assert Alert popup text and close
如果您使用 selenium IDE,则必须手动单击“确定”按钮,因为当运行警报消息命令时浏览器停止工作,如果您想自动单击“确定”按钮,则必须使用 selenium RC 或 webdriver,以下命令适用于 Selenium IDE
在selenium ide中使用
storeeval
命令,不同类型的盒子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您可以查看
chooseOkOnNextConfirmation
,尽管如果我正确阅读文档,这可能应该是默认行为。You might look into
chooseOkOnNextConfirmation
, although that should probably be the default behavior if I read the docs correctly.问题不清楚 - 这是页面加载时的警报吗?使用 Selenium 时您不应该看到任何警报对话框,因为它用自己的版本替换了
alert()
,该版本仅捕获为验证而给出的消息。Selenium 不支持页面加载时的
alert()
,因为它需要使用自己的版本来修补测试窗口中的函数。如果您无法摆脱被测应用程序的加载警报,您应该考虑使用 GUI 自动化来单击生成的弹出窗口,例如 AutoIT(如果您使用的是 Windows)。
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.
使用警报界面,首先
switchTo()
发出警报,然后使用accept()
单击“确定”或使用dismiss()
取消它或
Use the Alert Interface, First
switchTo()
to alert and then either useaccept()
to click on OK or usedismiss()
to CANCEL itor
关于 Selenium IDE,我不是专家,但您必须在触发警报/确认对话框的事件之前添加“在下次确认时选择确定”行,如您在此屏幕截图中所示:
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:
assertAlert 应该可以解决问题。我在文档中看到,在页面的 OnLoad 事件处理程序中生成的警报不能以这种方式编写脚本(唉,由于 ASP.NET 页面生命周期,我自己也经历过这种情况)。这可能就是你遇到的情况吗?
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?
对于 selenium,警报是使用 javascript 引发的警报,例如,
有一项基本检查来验证您的警报是否实际上是 javascript 警报,或者只是一个用于显示某些消息的基于 div 的框。
如果它是 javascript 警报,则在运行 selenium 脚本时您将无法在屏幕上看到它。
如果您能够看到它,那么您需要获取警报的“确定”按钮的定位器并使用 selenium.click(locator) 来关闭警报。如果您可以提供更多上下文,可以更好地帮助您:
瓦米普
For selenium, an alert is the one which raised using javascript e.g.
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:
Vamyip
使用 ChooseOkOnNextConfirmation() 消除警报,并使用 getAlert() 验证警报是否已显示(并可选择抓取其文本进行验证)。
Use chooseOkOnNextConfirmation() to dismiss the alert and getAlert() to verify that it has been shown (and optionally grab its text for verification).
对我有用的一个选项是:
An Option that worked for me is:
新的 Selenium IDE(2019 年发布)拥有更广泛的 API 和新文档。
我相信这是您想要尝试的命令:
webdriver在可见确认上选择确定
描述于:
https://www.seleniumhq.org/selenium-ide/docs/en/api/commands/#webdriver -choose-ok-on-visible-confirmation
还有其他与警报相关的 API 调用;只需在该页面搜索警报
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