When to use explicit wait vs implicit wait in Selenium Webdriver?

0
0

I am using:

driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS);

But it still fails continuously for the below element

    driver.findElement(By.id("name")).clear();
driver
.findElement(By.id("name")).sendKeys("Create_title_01");

I have added wait code:

for (int second = 0;; second++) {
if (second >= 120) fail("timeout");
try { if (isElementPresent(By.id("name"))) break; } catch (Exception e) {}
Thread.sleep(1000);
}

Shouldn’t implicit wait take care of waiting till an element is found?
Also would it be better if I use Explicit wait instead of the code I have added that has Thread.sleep()?

  • You must to post comments
0
0

TL;DR: Always use explicit wait. Forget that implicit wait exists.


Here is a quick rundown on the differences between explicit and implicit wait:

Explicit wait:

  • documented and defined behaviour.
  • runs in the local part of selenium (in the language of your code).
  • works on any condition you can think of.
  • returns either success or timeout error.
  • can define absence of element as success condition.
  • can customize delay between retries and exceptions to ignore.

Implicit wait:

  • undocumented and practically undefined behaviour.
  • runs in the remote part of selenium (the part controlling the browser).
  • only works on find element(s) methods.
  • returns either element found or (after timeout) not found.
  • if checking for absence of element must always wait until timeout.
  • cannot be customized other than global timeout.


Let’s see the difference between explicit wait and implicit wait in the actual source code of selenium. I copied the code from the python binding of selenium because python is “easy to read”.

The code of WebDriverWait.until() (explicit wait):

def until(self, method, message=''):
end_time
= time.time() + self._timeout
while(True):
try:
value
= method(self._driver)
if value:
return value
except self._ignored_exceptions:
pass
time
.sleep(self._poll)
if(time.time() > end_time):
break
raise TimeoutException(message)

Now in human language: explicit wait expects a method which returns a truish value if successful. It then repeatedly executes the given method with a delay in between. Expected errors from the given method are supressed. If the given method returns a truish value then explicit wait will return that value. If the time runs out a timeout exception is raised.

Compare to the code of WebDriver.implicitly_wait() (comments removed for brevity):

def implicitly_wait(self, time_to_wait):
self
.execute(Command.IMPLICIT_WAIT, {'ms': float(time_to_wait) * 1000})

self.execute() is WebDriver.execute() which calls RemoteConnection.execute() which in turn does, as far as I can tell, an RPC to the remote side of selenium.

In human language: the implicit wait sends a message to the “remote side” of the selenium webdriver. The remote side of the selenium webdriver is the part of selenium which is actually controlling the browser. What does the remote side do with the message? “It depends”. It depends on the operating system and on the browser and on the version of selenium. As far as I can tell there is no guarantee about the actual behaviour of a specific implementation.

Possible implementations are:

  • repeatedly try to find element until timeout. return as soon as element is found.
  • try to find element. wait until timeout. try again.
  • wait until timeout. try to find element.

Note that implicit wait only takes effect on find element(s) methods.

I have not looked up the actual source code of the remote sides of selenium. The information is gathered from reading the comments in the bug reports about implicit and explicit wait in selenium:

My conclusion: Implicit wait is bad. The capabilities are limited. The behaviour is undocumented and implementation dependent.

Explicit wait can do everything implicit wait can and more. The only disadvantage of explicit wait is a bit more overhead because of multiple remote procedure calls. Also the explicit wait is a bit more verbose. But that verbosity makes the code explicit. And explicit is better that implicit. Right?


Further reading:

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website