Grumbunks
Grumbunks

Reputation: 1267

Questions about implicit waits in Selenium Webdriver

I'm reading the docs on Implicit Waiting with Webdriver but I'm not sure I totally understand.

As I understand it,

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

This will put in place a timeout of 10 seconds whenever looking up any element.

What, exactly, does this do?

Upvotes: 2

Views: 158

Answers (2)

Guy
Guy

Reputation: 50809

This will look for the element up to 10 seconds, trying to locate it every 500 milliseconds (default timeout).

From the docs

Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.

When searching for a single element, the driver should poll the page until the element has been found, or this timeout expires before throwing a NoSuchElementException. When searching for multiple elements, the driver should poll the page until at least one element has been found or this timeout has expired.

The locating algorithm is described in W3C specifications

The Find Element, Find Elements, Find Element From Element, and Find Elements From Element commands allow lookup of individual elements and collections of elements. Element retrieval searches are performed using pre-order traversal of the document’s nodes that match the provided selector’s expression. Elements are serialized and returned as web elements.

When required to find with arguments start node, using and value, a remote end must run the following steps:

  1. Let end time be the current time plus the session implicit wait timeout.

  2. Let location strategy be equal to using.

  3. Let selector be equal to value.

  4. Let elements returned be the result of trying to call the relevant element location strategy with arguments start node, and selector.

  5. If a DOMException, SyntaxError, XPathException, or other error occurs during the execution of the element location strategy, return error invalid selector.

  6. If elements returned is empty and the current time is less than end time return to step 4. Otherwise, continue to the next step.

  7. Let result be an empty JSON List.

  8. For each element in elements returned, append the serialization of element to result.

  9. Return success with data result.

implicitlyWait is defined once for the WebDriver and last its lifetime.

Upvotes: 4

Yalcin Batur
Yalcin Batur

Reputation: 59

Defines a wait time globally in your project. Your telling your driver to wait for n number of seconds before selenium throws an exception. If element is found earlier then the n number of seconds you mentioned, webdriver will click it once its available and dont wait for the maximum n number of seconds. It does have to wait before it throws an exception.

Upvotes: 2

Related Questions