模态对话框存在异常

时间:2013-08-20 13:36:26

标签: java selenium webdriver selenium-webdriver

我尝试通过这种方式点击a元素:

WebElement title = driver.findElement(By.xpath("//a[text()=\"Delete this document library\"]"));
title.click();

当我手动点击它时,它会打开一个窗口:

The page at http://.. says
Are you sure you want to delete the selected items?

使用OKCancel按钮

但是当我使用WebDriver运行它时(在Firefox 20.0中),我得到了下一个错误:

[Exception]: Modal dialog present

我甚至看不到窗户。

可能是什么原因?

4 个答案:

答案 0 :(得分:4)

运行测试时没有看到警报,因为{em>模式对话框存在异常时,WebDriver的默认行为是接受警报 。它发生得太快,你看不到警报。

  WebElement title = driver.findElement(By.xpath("//a[text()=\"Delete this document library\"]"));
title.click();

//Now the alert appears. 
Alert alert = driver.switchTo().alert();
alert.accept();

答案 1 :(得分:3)

如果每次都没有出现警告窗口,您可以这样做:

try { 
    Alert alert = driver.switchTo().alert();
    alert.accept();
    //if alert present, accept and move on.
}
catch (NoAlertPresentException e) {
    //do what you normally would if you didn't have the alert.
}

答案 2 :(得分:1)

答案 3 :(得分:0)

alert.dismiss()或者按Esc按钮可以解除警报。对于我的情况,解决了这个问题。