13

According to selenium, an implicit wait polls the DOM for a certain amount of time to see if an element shows up. My understanding is that it will poll up to a specified amount of time, but if an element shows up before, then it will continue without waiting further.

http://seleniumhq.org/docs/04_webdriver_advanced.html

I have a method that runs in about 13 seconds. When I set the implicit wait to 100 seconds, it takes 213 seconds.

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

It appears that during this method, I'm waiting 2 times (100 seconds each). Setting the implicit wait to 0 or 100 doesn't affect my method. In both cases, they finish correctly.

My question is this. I thought the implicit wait waits for the shortest amount of time for an element to show up. Is this right? Or am I doing something wrong?

Furthermore, why is it waiting 2 times, when it apparently does not need to wait? (My method finishes correctly even if I set the wait to 0)

Mechlar
  • 4,946
  • 12
  • 58
  • 83
Steven
  • 714
  • 2
  • 8
  • 21
  • possible duplicate of [selenium webdriver - explicit wait vs implicit wait](http://stackoverflow.com/questions/10404160/selenium-webdriver-explicit-wait-vs-implicit-wait) – Lesmana Jan 22 '15 at 08:42

1 Answers1

9

Short answer:

implicit wait - It's global setting applicable for all elements and if element appear before specified time than script will start executing otherwise script will throw NoSuchElementException. Best way to use in setup method. Only affect By.findelement().

Thread.sleep() - It will sleep time for script, not good way to use in script as it's sleep without condition.

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
Anand Somani
  • 801
  • 1
  • 6
  • 15
  • What if the driver goto another url, is the implicit wait still effective? or do I need to recall it again? –  Jul 02 '14 at 20:45
  • ma, effective for the duration of the run regardless of URL. Note that for findElements it is possible to expect size() == 0. But Selenium will wait the last specified time. If the page is loaded at the time of the test, reducing the implicit wait time (e.g. 1 second) before the findElements and then resetting back to normal afterwards will lead to faster execution times. – Mike Jr Jun 02 '15 at 14:47