Rob G
Rob G

Reputation: 105

Selenium 4 and WebDriver implicitlyWait

In our 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?

The exception:

org.openqa.selenium.TimeoutException: java.util.concurrent.TimeoutException
Build info: version: '4.3.0', revision: 'a4995e2c09*'
System info: host: 'DESKTOP-8Q1PEBT', ip: '172.32.6.43', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.15'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Command: [e8d45a02973053acad1a896d2ed471e6, setTimeout {implicit=0}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 103.0.5060.134, chrome: {chromedriverVersion: 103.0.5060.134 (8ec6fce403b..., userDataDir: C:\Users\jenkins\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:59791}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://172.32.6.43:4444/sessi..., se:cdpVersion: 103.0.5060.134, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: e8d45a02973053acad1a896d2ed471e6
    at org.openqa.selenium.remote.http.netty.NettyHttpHandler.makeCall(NettyHttpHandler.java:65)

I still see following in Chrome 104

10:48:48 Build info: version: '4.3.0', revision: 'a4995e2c09*'
10:48:48 System info: host: 'DESKTOP-8Q1PEBT', ip: '172.32.6.43', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.15'
10:48:48 Driver info: org.openqa.selenium.remote.RemoteWebDriver
10:48:48 Command: [f97c4a4cd7a0d468391badd20e23fe8f, setTimeout {implicit=240000}]
10:48:48 Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 104.0.5112.81, chrome: {chromedriverVersion: 104.0.5112.79 (3cf3e8c8a07d..., userDataDir: C:\Users\jenkins\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:56549}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://172.32.6.43:4444/sessi..., se:cdpVersion: 104.0.5112.81, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
10:48:48 Session ID: f97c4a4cd7a0d468391badd20e23fe8f]
10:48:48 org.openqa.selenium.TimeoutException: java.util.concurrent.TimeoutException

Upvotes: 0

Views: 4392

Answers (2)

Rob G
Rob G

Reputation: 105

Still not fixed in ChromeDriver. :(

I decided the only solution was to catch the exception and try again as @pcalkins suggested.

    public void setImplicitWait(int maxWaitTime) {
        try {
            this.driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(maxWaitTime));
        } catch (TimeoutException e) {
            this.driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(maxWaitTime));
        }
    }

Of course there could be a counter and a max retry, but so far I've found just 1 retry and it never fails.

I ended up wrapping a whole bunch of basic methods (e.g., findElement) in the same manner.

I know it is crude but so far seems to be my solution.

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193058

To bell the cat as per the Selenium Documentation:

Warning: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.


Solution

To simulate the expectation for checking for the element to be invisible you can use either of the following approaches:

  • Using invisibilityOf(WebElement element):

    new WebDriverWait(driver, Duration.ofSeconds(waitTime)).until(ExpectedConditions.invisibilityOf(driver.findElement(By.cssSelector("ElementCss"))));
    
  • Using invisibilityOfElementLocated(By locator):

    new WebDriverWait(driver, Duration.ofSeconds(waitTime)).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("ElementCss")));
    

Upvotes: 0

Related Questions