2010-08-31 56 views
6

如何通过点击“确定”按钮来关闭提醒框?我在WebDriver Google小组上看到Simon正在添加此功能。我不确定这是否得到支持。WebDriver关闭提醒框

回答

17
driver.findElement(By.id("updateButton")).click(); 

//pop up with id "updateButton" opens

Alert alert = driver.switchTo().alert(); 
//update is executed 
alert.accept(); 
+0

这是做这件事的正确方法,特别是在2012年前后。我不明白为什么这不作为问题的答案被检查,因为它应该是。 – djangofan 2013-02-14 19:20:31

8

看看issue 27 in the Google Code Issues List。使用JavaScriptExecutor覆盖浏览器的警报:

((JavascriptExecutor)driver).executeScript("window.alert = function(msg){};"); 

这是一个类似的解决方案来处理确认对话框。

((JavascriptExecutor)driver).executeScript("window.confirm = function(msg){return true;};"); 

这是一个非常受欢迎的请求,所以希望它能尽快实施。这些方法的缺点是验证消息中的文本并不容易。你可能想要下令Selenium来做到这一点。

+0

我已经试过'((JavascriptExecutor)驱动程序) .executeScript(“window.confirm = function(msg){return true;};”);'但没有运气。似乎没有人有可靠的解决方法。我真的希望他们能很快完成这个工作。不管怎么说,还是要谢谢你。 – zihaoyu 2010-09-02 17:56:10

+0

仔细检查您的驱动程序是否引用了正确的页面/框架。我已经成功地运行它,所以它必须是你的页面唯一的东西,而不是代码。 – pnewhook 2010-09-08 19:23:44

2

C#代码:

IAlert alert = driver.SwitchTo().Alert(); 
alert.Accept(); 
System.Threading.Thread.Sleep(milliseconds); 
1

Python代码:

driver.switch_to_alert().accept() 

参见:

站点包/硒/ webdriver的/远程/ webdriver.py

站点包/硒/ webdriver的/通用/ alert.py