Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium - Is it okay to mix implicit wait and explicit wait like this?

Here is a udemy course (from "Lets Kode It") to develop a web automation framework with selenium and Java. But, this is not a java question. You only need to know selenium in any of these languages - javascript, python, ruby, c# & java.

The instructor has developed a CustomDriver class which has the method/function given below. The method waits for an element to be clickable, without having to write WebDriverWait statements everywhere in our code. It first sets the implicit wait to zero, does an explicit wait and then sets the implicit wait to the original value which was being used in the framework.

This approach seems okay to me, but I am not sure about it. Could mixing implicit and explicit waits like this cause any problems?

UPDATE (March 24, 2020) - I already know that mixing implicit and explicit waits is considered a bad practice because it can lead to unpredictable wait times. I am not asking about the unpredictable wait times because there are plenty of questions and articles on that already.

Instead, I am asking that if the implicit wait is set to zero every time before doing an explicit wait, then is that okay? Will that still cause the problems of unpredictable waits? Will it cause other problems ?

/*
 Click element when element is clickable
 @param locator - locator strategy, id=>example, name=>example, css=>#example,
                       tag=>example, xpath=>//example, link=>example
 @param timeout - Duration to try before timeout
 */
public void clickWhenReady(By locator, int timeout) {
    try {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
        WebElement element = null;
        System.out.println("Waiting for max:: " + timeout + " seconds for element to be clickable");

        WebDriverWait wait = new WebDriverWait(driver, 15);
        element = wait.until(
                ExpectedConditions.elementToBeClickable(locator));
        element.click();
        System.out.println("Element clicked on the web page");
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    } catch (Exception e) {
        System.out.println("Element not appeared on the web page");
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }
}
like image 683
armani Avatar asked Sep 17 '25 05:09

armani


1 Answers

I would not suggest mix them. As Implicit wait is most of the times implemented at the remote side of the WebDriver system meaning they are handled in the browser based driver (eg: chromedriver.exe, IEDriverServer.exe), where as Explicit Wait is implement on the local language bindings like Java, ruby, python etc.

Below is the typical example what happens when your run the script with remote server.

Local Code -> Remote server -> Local language bindings on the remote server -> remote component like chromedriver.exe or IEDriverServer.exe. Things get more complex when if you have grid involved as it could be another layer in between the chain.

So when you mix both the Implicit and Explicit waits, you might end-up with undefined behavior. And moreover, as Implicit waits are implemented at the driver level they might change anytime and have impact on your scripts. So, it's always better to stick to Explicit wait and have total control.

With the current technologies, the elements might render lately after the element is present. So, going with implicit wait is not sufficient, hence strongly prefer using explicit wait. There may be some edge cases we might have to use implicit wait, but never mix both of them if you are planning to extend your script in future to run on grid/using remote server.

like image 90
supputuri Avatar answered Sep 23 '25 12:09

supputuri