在使用Chrome驱动程序,并尝试测试一个网页的时候,遇到以下异常:
org.openqa.selenium.UnhandledAlertException:意外警报打开
(会话信息:chrome = 38.0.2125.111)
(驱动程序信息:chromedriver = 2.9.248315,platform = Windows NT 6.1 x86)(警告:服务器没有提供任何堆栈跟踪信息
命令持续时间或超时:16毫秒:null
构建信息:version:'2.42.2',revision:'6a6995d',time:'2014-06- 03 17:42:30'
系统信息:主机:'Casper-PC',ip:'10.0.0.4',os.name:'Windows 7',os.arch:'x86',os.version :'6.1',java.version:'1.8.0_25'
驱动程序信息:org.openqa.selenium.chrome.ChromeDriver
使用如下代码尝试处理异常:
警报alt = driver.switchTo().警报();
alt.accept();
这次又报出org.openqa.selenium.NoAlertPresentException异常,
解决方法:
由于驱动程序遇到警报时的默认行为。默认行为设置为“ACCEPT”,因此警报自动关闭,switchTo().alert()找不到。
修改驱动程序的默认行为(“IGNORE”),以使其不会关闭警报:
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,UnexpectedAlertBehaviour.IGNORE);
d = new FirefoxDriver(dc);
然后可以处理该异常:
try {
click(myButton);
} catch(UnhandledAlertException f){
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println("警报数据:"+ alertText);
alert.accept();
} catch(NoAlertPresentException e){
e.printStackTrace();
}
}
版权声明:本文著作权归原作者所有,欢迎分享本文,谢谢支持!
转载请注明:org.openqa.selenium.UnhandledAlertException:意外警报打开(org.openqa.selenium.UnhandledAlertException: unexpected alert open) | 雨晨博客
转载请注明:org.openqa.selenium.UnhandledAlertException:意外警报打开(org.openqa.selenium.UnhandledAlertException: unexpected alert open) | 雨晨博客