3

When my test run using a webdriver for Firefox I'll get modal exception after reaching an accept on an alert:

[Exception]: Modal dialog present

The tests however work fine when using IE webdriver. The problem seems to be that test scenario proceeds before the alert is completely closed. In other words it tried to do the following:

List<WebElement> l = driver.findElements(By.linkText("link"));

and a result I'll get modal exception.

Now wait.until(ExpectedConditions) as mentioned wait.until(ExpectedConditions.visibilityOf Element1 OR Element2) is good candidate. However I am not able to create a condition that returns true when alart disappears. How can I do that?

Edit: to clarify further, the problem is not waiting for the alert to appear (my implicit wait is working fine for that purpose). I do get the alert and apply the test on that however after I click accept on the alert, test goes on fast and tried to proceed with the next step while alert still present and throws the modal present exception. I have also tried the following to prevent that with out success.

driver.switchTo().defaultContent();
Community
  • 1
  • 1
Learner4
  • 31
  • 3
  • I am having the same issue with FF. The modal dialog is still present when attempting to do the next step. – Bleeped Aug 04 '15 at 17:03
  • I'm having the same problem, I used WebDriverWait().until(ExpectedConditions.InvisibilityOf(Element)) however this is causing longer wait times than what I wan to deal with. – Riana Feb 09 '17 at 22:32

2 Answers2

0

There is now default mechanism to wait for alert to appear/disappear but, we can write our own logic something like below instead of waiting for static amount of time (Thread.sleep(10000)).

waitForAlert(WebDriver driver)
{
   int i=0;
   while(i++<5)
   {
        try
        {
            Alert alert = driver.switchTo().alert();
            alert.accept();
            break;
        }
        catch(NoAlertPresentException e)
        {
          Thread.sleep(1000);
          continue;
        }
   }
}
Santoshsarma
  • 5,627
  • 1
  • 24
  • 39
  • Thread.sleep because he told alert not appeared/disappeared properly after clicking on something.SO, before proceeding with next step he needs to handle that alert. If alert is appearing some delay we need to wait. right? for that only I mentioned Thread.sleep. I hope you understand what I'm trying to tell. – Santoshsarma May 30 '14 at 05:18
  • Removing `Thread.sleep` and increasing counter `i` will be a better option. – Ajinkya May 30 '14 at 05:36
  • Yeah. I agree with you Karna. – Santoshsarma May 30 '14 at 07:48
  • I further clarified the question as the problem is not waiting for alert to appear. It is done with my implicit wait and no problem with that. The problem is that I can't prevent the test from proceeding while the alert is present. – Learner4 May 30 '14 at 14:53
  • Test will fail if you dont habdle alert. if you wait for alert and accept if appears or contine if dont it dont appear then test will be fine. – Ajinkya May 31 '14 at 08:19
0

even I faced this problem in FF and I came over this by using a AUI. Try to use the below code to get your alert > accept it and then continue with rest of your code/test.

Actions action = new Actions (driver);
action.click(driver.findElement(By.id("locator"))).build().perform();
driver.switchTo().alert().accept();
// Continue with your test
List<WebElement> l = driver.findElements(By.linkText("link"));
Babulu
  • 334
  • 4
  • 16