Selenium NoAlertPresentException Selenium NoAlertPresentException selenium selenium

Selenium NoAlertPresentException


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


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


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. :)