Selenium WebDriver Wait: A Detailed Guide

In automation, waiting is having the automated task execution elapse a certain amount of time before continuing with the next step. Selenium WebDriver provides various commands for implementing wait. In this post, we have listed out all the Selenium WebDriver wait commands for your reference.selenium webdriver wait

Conceptually, Selenium WebDriver wait commands can be divided in various different categories. These are implicit wait, explicit wait, timeout, expected conditions, fluent wait etc. We will go through each one of them with their usage in this post. Whenever possible, we have provided code snippets for clear understanding.

Implicit Wait:
Implicit wait tells Selenium instance to wait for specified amount of time before throwing an exception. The default setting is 0. Let’s see how implicit wait works. While execution, if WebDriver cannot find element to act upon immediately, it will wait for specified amount of time. During this time, no attempt is made to find an element. After completion of specified time, WebDriver will try to find an element once. Exception is displayed if element is not found after that.

Once set, the implicit wait is set for the life of the WebDriver object instance. That means it will be available for the entire time the browser is open. Implicit wait can be implemented by using below code.

Explicit Wait:
As the name suggests, we explicitly instruct WebDriver for waiting by using explicit wait. By using custom coding, we tell Selenium WebDriver to wait for some expected condition.

Why use explicit wait when implicit wait is in place and doing good already? Glad you asked. Well, you may encounter instances when some element takes more time to load. Setting implicit wait for such cases doesn’t make sense as browser will wait unnecessarily for the same time for every element, increasing the automation time. Explicit wait helps here by bypassing implicit wait altogether for some specific elements.

Now let’s see how explicit wait works. WebDriver provides WebDriverWait and ExpectedConditions classes to implement explicit wait. Below code shows using WebDriverWait and ExpectedConditions for explicit wait. In this code, WebDriver will wait for 10 seconds before throwing a TimeoutException. If element is found, it will be returned in 0-10 seconds.

WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. ExpectedConditions class has some predefined common conditions to wait for an element. Click here to see list of these conditions in Java binding.

Fluent Wait:
Unlike implicit and explicit wait, fluent wait uses two parameters. Timeout value and polling frequency. Let’s say we have timeout value as 30 seconds and polling frequency as 2 seconds. WebDriver will check for element for every 2 seconds until timeout value (30 seconds). After timeout value is exceeded without any result, exception is thrown. Below is a sample code which shows implementation of fluent wait.

Another advantage of using fluent wait is, we can ignore specific types of exceptions (Eg. NoSuchElementExceptions) while waiting. Due to all these provisions, fluent wait is helpful in AJAX applications as well as in scenarios when element load time fluctuates often. Strategic use of fluent wait significantly improves automation efforts.

Apart from waits mentioned about, Selenium WebDriver provides timeouts. There are two timeouts supported: pageLoadTimeout and setScriptTimeout. Let’s go through each one of them.

pageLoadTimeout: It sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite. Use below code to implement pageLoadTimeout.

setScriptTimeout: It sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely. Use below code to implement setScriptTimeout.

Using sleeper in your Selenium tests is not a good practice and should be avoided. Reason for this is, it forces browser to wait for specified amount of time. It unnecessarily delays other automation steps. You can implement sleeper in your test with below code.

Here time value is in milliseconds.

You should always consider context while using Selenium WebDriver wait in your automation activities. Proper usage of waits would make your test robust and efficient. With that we hope our coverage of different Selenium WebDriver wait above will be helpful to you. Let us know your views on this.

  1. Mohan R
  2. Jaypal
    • Santiago
  3. akhilesh
  4. jayakumar
    • venkatesh
  5. reddy

Leave a Reply

Your email address will not be published. Required fields are marked *