org.openqa.selenium.UnhandledAlertException:意外警报打开 [英] org.openqa.selenium.UnhandledAlertException: unexpected alert open

查看:4560
本文介绍了org.openqa.selenium.UnhandledAlertException:意外警报打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Chrome驱动程序,并尝试测试一个网页。

I am using a Chrome Driver and trying to test a webpage.

通常运行正常,但有一段时间我得到例外 -

Normally it runs fine but some time I gets exceptions--

 org.openqa.selenium.UnhandledAlertException: unexpected alert open
 (Session info: chrome=38.0.2125.111)
 (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 x86) (WARNING: The server did not  provide any stacktrace information)
 Command duration or timeout: 16 milliseconds: null
 Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:30'
 System info: host: 'Casper-PC', ip: '10.0.0.4', os.name: 'Windows 7', os.arch: 'x86', os.version:  '6.1', java.version: '1.8.0_25'
 Driver info: org.openqa.selenium.chrome.ChromeDriver

然后我尝试处理警报 -

Then i tried to handle the alert --

  Alert alt = driver.switchTo().alert();
  alt.accept();

但这次我收到了 -
org.openqa.selenium.NoAlertPresentException

But this time i recived-- org.openqa.selenium.NoAlertPresentException

我附加了警报的屏幕截图 -

I am attaching the screen shots of the alert-

我无法弄明白现在要做什么。问题是我总是不会收到这个异常。当发生这种情况时,测试失败。

I am not able to figure out what to do now. The problem is i am not receiving this exception always. And when it occurs then the test fails.

推荐答案

我也有这个问题。这是由于驱动程序遇到警报时的默认行为。默认行为设置为ACCEPT,因此警报自动关闭,switchTo()。alert()找不到。

I had this problem too. It was due to the default behaviour of the driver when it encounters an alert. The default behaviour was set to "ACCEPT", thus the alert was closed automatically, and the switchTo().alert() couldn't find it.

解决方案是修改驱动程序的默认行为(IGNORE),以使其不会关闭警报:

The solution is to modify the default behaviour of the driver ("IGNORE"), so that it doesn't close the alert:

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("Alert data: " + alertText);
        alert.accept();
    } catch (NoAlertPresentException e) {
        e.printStackTrace();
    }
}

这篇关于org.openqa.selenium.UnhandledAlertException:意外警报打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆