在 Selenium WebDriver (Selenium 2) 中处理警报
driver.findElement(By.xpath("//input[@value='添加']")).click();
//Pops out an Alert and program stops, does not continue
如何点击提醒?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
啊啊啊啊 怎么没有人呢? (TRANS: ahahahahaha why there is no one here to reply my post?)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我顶 (TRANS: Let me promote this post!)
driver.findElement(By.xpath("//input[@value='添加']")).click();
//Pops out an Alert and program stops, does not continue
how to click the alert?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
啊啊啊啊 怎么没有人呢? (TRANS: ahahahahaha why there is no one here to reply my post?)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我顶 (TRANS: Let me promote this post!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您想取消弹出窗口,请使用以下命令:
而不是
If you want to cancel the pop up use the following:
instead of
从最新的 selenium 2 版本开始,可以做到这一点(至少使用 FirefoxDriver):
As of the latest selenium 2 release, this can be done (at least using the FirefoxDriver):
在 Selenium 2 中,当前警报仅在 Firefox 浏览器中处理。您无需指定测试所使用的语言,但以下是如何使用 ruby 处理警报。 (这取自 selenium wiki 上的 Ruby Bindings 页面)。
Javascript 警报/确认
您可以使用 webdriver 来处理 javascript 警报和确认对话框。两者的实现是相同的。
注意:目前该 API 仅在 Firefox 中可用(或在使用远程服务器的 Firefox 中),并且只能捕获加载后生成的警报/确认。
In Selenium 2, currently alerts are only handled in the Firefox browser. You don't specify what language you're using for your tests, but here's how to handle an alert using ruby. (This is taken from the Ruby Bindings page on the selenium wiki).
Javascript Alert/Confirm
You can use webdriver to handle javascript alert and confirm dialogs. The implementation for both is the same.
Note: At this time the API is only available in Firefox (or in Firefox using the remote server), and only alert/confirms that are generated post load can be captured.
在 Selenium 2 的早期版本中,我没有选择通过覆盖 Javascript 中的 window.alert 来处理 Internet Explorer 中的警报:
Selenium 2.0b3 支持在 IE 和 Firefox 中处理警报,因此您可以执行以下操作:
但是,我无法使上述内容与是/否警报一起使用(Dismiss() 适用于“否”,但 Accept() 不适用于“是”)。我正在查看 IEDriver 以找出原因。
In previous version of Selenium 2 I have had no choice that to handle alerts in Internet Explorer by overriding the window.alert in Javascript:
Selenium 2.0b3 has support for handling Alerts in IE and Firefox, so you can do the following:
However, I have not been able to get the above to work with Yes/No alerts (Dismiss() works for No but Accept() doesn't work for Yes). I'm in the process of looking at the IEDriver to work out why this is.
您必须处理异常并运行警报处理程序代码,对于 Java:
捕获此异常,然后您可以相应地接受或拒绝警报。
You will have to handle exception and run your handler code for Alert, for Java:
Catch this exception, and then you can accordingly accept or reject alert.
C# 代码:
C# code: