简体   繁体   中英

Modal dialog present Exception

I try to click on a element, by this way:

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

When I click on it manually, its opens a window:

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

with OK and Cancel buttons

But when I run it with WebDriver (in Firefox 20.0 ), I get the next error:

[Exception]: Modal dialog present

and I don't see even the window.

What can be the reason?

You don't see the Alert when you run the test is because the default behavior of the WebDriver is that it accepts alert when the Modal dialog present exception is thrown . It happens so fast that you can't see the alert.

  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();

If an alert window doesn't appear every time, you could do this:

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.
}

alert.dismiss() or or Press Esc button you can dismiss the alert. For my case for this is solved this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM