Selenium 4 and WebDriver implicitlyWait

In out Selenium projects we use Implicit waits and fluent waits. Basically, we use Implicit waits until we need something explicit. E.g., for normal findElements() calls we depend on the implicit wait, but when waiting for an element to disappear after clicking some button we use a fluent wait. (See code below).

The problem is that lately I’m seeing a number of timeouts when calling implicityWait(n). This seems new. Maybe even in Selenium 4.3. Cannot say for sure, but it has certainly become bothersome. To be clear, “occasionally” means a couple of times over a couple of hundred tests (each calling implicitlyWait dozens of times).

public void waitUntilElementDisappear(By locator, int waitTime) throws Exception, TimeoutException {
    Logger.log(String.format("Waiting [%ds] for [%s] to disappear", waitTime, locator));
    try {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(0));
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(waitTime));
        wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));
    } catch (TimeoutException e) {
        Logger.log("Caught timeout exception");
        throw e;
    } catch (Exception e) {
        Logger.log(String.format("Caught exception message:[%s]", e.getMessage()));
        throw e;
    } finally {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(DEFAULT_WAIT));
    }
}

The issue occurs only in the finally block. (DEFAULT_WAIT is usually 180).

Any suggestions?

Read more here: Source link