Java 在 Chrome 驱动程序 (Webdriver) 上处理警报窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18963678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Handling Alert Windows on Chrome Driver (Webdriver)
提问by elcharrua
im having trouble handle ALert pop ups from Chrome, I keep getting the following error. org.openqa.selenium.UnhandledAlertException: unexpected alert open (Session info: chrome=29.0.1547.66) (Driver info: chromedriver=2.3,platform=Windows NT 5.1 SP3 x86) .
我在处理来自 Chrome 的 ALert 弹出窗口时遇到问题,我不断收到以下错误。org.openqa.selenium.UnhandledAlertException:意外警报打开(会话信息:chrome=29.0.1547.66)(驱动程序信息:chromedriver=2.3,平台=Windows NT 5.1 SP3 x86)。
Here is what i have tried so far. WHen i get to the page where the error is displayed:
这是我迄今为止尝试过的。当我到达显示错误的页面时:
driver.switchTo().alert.accept();
Also tried.
也试过了。
Alert alert = driver.switchTo().alert();
alert.accept();
and also the same error.
还有同样的错误。
If any got a solution for this it will be appreciated.
如果有人对此有解决方案,将不胜感激。
采纳答案by NaviSaysListen
It may be your ChromeDriver version. I wouldn't recommend always updating to the newest versions of something. Defects abound.
它可能是您的 ChromeDriver 版本。我不建议总是更新到最新版本的东西。缺陷比比皆是。
I'm using ChromeDriver win32_2.0 and it works fine. Try that version.
我正在使用 ChromeDriver win32_2.0,它工作正常。试试那个版本。
回答by Santoshsarma
Actually it(UnhandledAlertException) comes if you don't handle alert properly Otherwise, If you do any operation with driver instance before closing the alert.
实际上它(UnhandledAlertException) 如果您没有正确处理警报就会出现,否则,如果您在关闭警报之前对驱动程序实例进行了任何操作。
Example
例子
Step-1: Click Button //this will lead to get an alert
步骤 1:单击按钮 //这将导致收到警报
Step-2: //Here you need to alert handle
Step-2: //这里你需要提醒句柄
In step-2 instead of handling alert if you do any other operation with driver instance it will throw UnhandledAlertException exception.
在第 2 步中,如果您对驱动程序实例执行任何其他操作,而不是处理警报,它将抛出 UnhandledAlertException 异常。
回答by Bart Wojtala
I got this problem with IE. But with two simple changes it started to work like under FF:
1)As suggested https://stackoverflow.com/a/20611297/2872258I set another option while creating IEDriver - unexpectedAlertBehaviour=Ignore
2)I had also WebDriverWait for Alert, with setting ImplicityWait to "0" at the very beginning - according what @Santoshsarma said it was another problem.
Might be a solution as well for Chrome.
我在 IE 上遇到了这个问题。但是通过两个简单的更改,它开始像在 FF 下一样工作:
1)按照https://stackoverflow.com/a/20611297/2872258 的建议,我在创建 IEDriver 时设置了另一个选项-unexpectedAlertBehaviour=Ignore
2)我也有 WebDriverWait for Alert,在一开始就将 ImplicityWait 设置为“0”-根据@Santoshsarma 所说,这是另一个问题。
也可能是 Chrome 的解决方案。
回答by user2155403
I tried catching the stackoverflow error and it worked for me as a workaround.
我尝试捕获 stackoverflow 错误,它作为一种解决方法对我有用。
try
{
driver.findElement(By.xpath('xpath')).click(); // command that will trigger the alert window
}
catch (StackOverflowError e)
{
driver.switchTo().alert().dismiss(); // or driver.switchTo().alert().accept();
// the rest of the scripts can be added here
}