简体   繁体   中英

Cannot catch alert modal using Java Selenium WebDriver 4

I'm automating this site as a practice https://www.demoblaze.com/index.html , after completing sign up modal and click Sign Up button, another pop up is raised to confirm the sign up process.

If I don't catch the alert modal I get "org.openqa.selenium.UnhandledAlertException: unexpected alert open: {Alert text: Sign up successful.}"

but if I handle the alert like this:

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        try {
            driver.switchTo().alert().accept();
        }
        catch (NoAlertPresentException Ex) {
            System.out.println("no hay alerta");
        }

I get "org.openqa.selenium.NoAlertPresentException: no such alert" When I debugged the test, it skips the try/catch sentence.

I also see that I cannot inspect with Chrome browser dev tools or Xpath Helper.

You should wait for the Alert using webDriver wait like below and then switch to it

new WebDriverWait(driver, Duration.ofSeconds(60)).ignoring(NoAlertPresentException.class)
            .until(ExpectedConditions.alertIsPresent());
    Alert alert = driver.switchTo().alert();
    alert.accept();

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