0

The Selenium documentation page on Waits says:

Implicit waiting for elements to appear is disabled by default and will need to be manually enabled on a per-session basis.

From this I deduce that explicit waits are generally preferred. I'd like to know why are explicit waits officially preferred over implicit waits?

urig
  • 16,016
  • 26
  • 115
  • 184
  • Just to add to confusion . Webdriver protocol only talk about implicit wait . Its only related to locating strategy (Specifies a time to wait for the element location strategy to complete when location an element(https://www.w3.org/TR/webdriver/#dfn-implicit-wait-timeout .There is nothing like explicit wait in webdriver protocol . Answer to Why to use explicit wait -> Explicit waits are invented by language binding side implementation to give greater control over wait conditions. – Rahul L Dec 23 '19 at 04:24

2 Answers2

2

Implicit wait continuously polls the DOM looking for a specific condition and it implies a common 'up to' time limit for all calls in a session. Explicit wait, on the other hand, specifies a 'up to' limit for a specific command.

As the Selenium documentation states, it is not recommended to mix implicit and explicit waits (see Combining implicit wait and explicit wait together results in unexpected wait times for more).

If you use implicit state anywhere, you would have to find an upper bound for the 'up to' limit. Sometimes, you would prefer to put a lower limit to a specific element (you may be testing page opening time) but not for other elements.

urig
  • 16,016
  • 26
  • 115
  • 184
Leandro Luque
  • 518
  • 3
  • 6
  • Thanks @leandro-luque. Can you provide links to show that indeed implicit wait simply waits for a defined amount of time rather than poll until a timeout? – urig Dec 22 '19 at 13:51
  • 1
    *Implicit wait is just a pause for a predefined amount of time* Not true. It's the maximum time to look for an element. – Guy Dec 22 '19 at 13:51
  • A good real example may be found here: http://elementalselenium.com/tips/47-waiting – Leandro Luque Dec 22 '19 at 14:09
  • Thanks again @leandro-luque but I don't see how this quite answers my question? Why is explicit officially preferred over implicit? – urig Dec 25 '19 at 10:46
0

Implicit wait is the maximum time for element lookup in the DOM.

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

Means WebElement element = driver.findElement(By.id("someId")) will look for element with id "someId" up to 10 seconds, and return this element as soon as it exists in the DOM.

Explicit wait use ExpectedConditions, which means you can wait for an element to be in a certain state

WebDriverWait wait = new WebDriverWait(WebDriverRefrence, 10);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("someId")));

Is similar, wait for an element to exist in the DOM, but there are many more options, for example

WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("someId")));

Which will wait for the element to be visible. It also give you options to wait for other conditions not related to locating elements, like waiting for url to change or to alert to appear

wait.until(ExpectedConditions.urlToBe("someUrl"));

wait.until(ExpectedConditions.alertIsPresent());
Guy
  • 46,488
  • 10
  • 44
  • 88