8

I'm trying to handle dialog (Ok Cancel type) with selenium WebDriver. So my aim is to click "Ok" button.

Scenario is:

  1. Click button for invoking dialog

    button.click();

  2. Try to accept

    webDriver.switchTo().alert().accept();

But I'm always getting NoAlertPresentException and seeing that dialog closes almost immediately. It seems to me that Selenium automatically closes dialog and when I want to accept, there is nothing to accept.

I'm sorry for my bad English.

4
  • 1
    Is it a timing issue? Commented Jul 19, 2013 at 14:58
  • Which driver are you using? For example SafariDriver will automatically dismiss all alerts because It cannot handle them. Commented Jul 22, 2013 at 8:52
  • Did you ever solve the issue? I have the exact same issue. And no amount of Google archaeology seem to help :) Commented Apr 8, 2014 at 12:07
  • @JacobR: Probably you can try to add some additional waiting time, as the answer suggests. Commented Jul 19, 2015 at 17:15

5 Answers 5

10

The usual cause of this issue is that Selenium is too quick and tries to accept an alert that has not yet been opened by the browser. This can be simply fixed by an explicit wait:

button.click();
WebDriverWait wait = new WebDriverWait(driver, 5);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.accept();
Sign up to request clarification or add additional context in comments.

Comments

1
Step 1:
    public boolean isAlertPresent(){
            boolean foundAlert = false;
            WebDriverWait wait = new WebDriverWait(driver, 0 /*timeout in seconds*/);
            try {
                wait.until(ExpectedConditions.alertIsPresent());
                foundAlert = true;
                System.out.println("isAlertPresent : " +foundAlert);
            } catch (TimeoutException eTO) {
                foundAlert = false;
                System.out.println("isAlertPresent : " +foundAlert);
            }
            return foundAlert;
        }

Step 2:
public boolean tocheck_POP_Dialog()
    {   Alert alert;
        try
        {   
            alert=driver.switchTo().alert();


        }
        catch(NoSuchElementException elementException)
        {   
            return false;
        }

        alert.accept(); //Close Alert popup


        return true;
    }




Step 3 :
if(dummyPage.isAlertPresent())
                {
                    dummyPage.tocheck_POP_Dialog();
                }

3 Comments

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
the first function returns whether there is a alert.
2nd function clicks on the alert ok button
0
public boolean isAlertPresent(){ 
    try{ 
        Alert a = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
        if(a!=null){
            System.out.println("Alert is present");
            driver.switchTo().alert().accept();
            return true;
        }else{
            throw new Throwable();
        }
    } 
    catch (Throwable e) {
        System.err.println("Alert isn't present!!");
        return false; 
    } 

} 

Use explicit wait to check the alert and then do the operation. This might help you. :)

Comments

0

Generally it happens because Selenium commands run too quick and it tries to close the alert before it is open. Hence, adding a delay after click event should resolve the issue. Also, if you are using Safari browser for your test, there is some issue with SafariDriver in handling alerts. SafariDriver cannot handle alerts should provide you more details.

Comments

0

Some additional info. for future readers of this thread:

If this exception persists even after the wait aspect is addressed, please check if the following sequence of steps is effective in the test script:

  1. the underlying Html page's DOM is queried/parsed for some purpose (e.g. to look for Form errors)

  2. (before the) driver.switch_to.alert is attempted

When an Alert is shown over an Html page, if the Alert is overlooked and the DOM underlying the Html page is queried first, the Webdriver appears to loose track of the Alert & causes the Exception.

This was observed with: geckodriver 0.21.0, Firefox 66.0b10 (64-bit) ; python 3.6.1 Selenium driver 3.14 (for Python).

Performing (2) before (1) was found to resolve the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.