Waits in Selenium Web Driver

September 21, 2016

While working on an automation project, the biggest challenge you will face is synchronization ie. syncing up automation scripts with the application under test. There are few web pages or web elements which load within no time and there are few which take comparatively longer time to load. In this tutorial, we will learn several types of wait statements that Selenium WebDriver offers.

In Selenium WebDriver, to sync up scripts there are four types of wait:

  • Page Load Timeout
  • Implicit Wait
  • Explicit Wait
  • Fluent Wait

1. PageLoadTimeout –

This is the maximum time selenium waits for a page to load successfully on a browser. If the page takes more than this time, it will throw Page not found Exception.

EXPLANATION- In the above code , pageLoadTimeout() method is accepting two arguments, one is waiting time and  in another, we are specifying the Time Unit.

Here Selenium WebDriver instance will wait a maximum of 90 seconds for a webpage to load. If it is loaded before the specified wait time, the execution will move to the next line of the script. If it doesn’t get loaded in 90 seconds it will throw Timeout Exception.

Now, let’s understand  how a Web page gets loaded in a Web browser?

  • WEB PAGE- It is a document commonly written in Hypertext Markup Language (HTML) that is accessible through the Internet or other network using an Internet browser. A web page is accessed by entering a URL address and may contain text, graphics, and hyperlinks to other web pages and files. The page you are reading now is an example of a web page.
  • WEB BROWSER- It knows how to go to a Web server on the Internet and request a page so that the browser can pull the page through the network and into your machine. A Web browser knows how to interpret the set of HTML tags within the page in order to display the page on your screen as the page’s creator intended it to be viewed.
  • WEB SERVER – A Web server is a piece of computer software that can respond to a browser’s request for a page, and deliver the page to the Web browser through the Internet.

How Webpage gets loaded

Fig. How page load works?

Whenever we type a URL in a browser, an HTTP request is triggered to the Web Server, which can be a GET or a POST request. WebServer responds to the browser with a GET response which has an attribute called content-length. The browser waits for the data which comes in chunks till data equal to this content-length attribute is received and then it generates an event trigger which signifies that page is loaded successfully. Selenium Page “load Timeout” command waits for this event trigger.


2. Implicit Wait –

Some Web Elements load very fast and others take time to load on the page. Implicit wait can be considered as element detection timeout. Once defined in a script, this wait will be set for all the Web Elements on a page.

  • Selenium script keeps polling to check whether that element is available to interact with or not.
  • It the maximum time, selenium code waits to interact with that Web Element, before throwing “Element not found exception”.

3. Explicit Wait

This wait can be considered as conditional wait, and is applied to a particular Web Element with a condition. There are many conditions which can be applied using explicit wait.

Say, for example, there is a Web Element on a page which takes more than expected time to appear on the page, so instead of increasing Implicit wait for a particular Web Element, we can apply explicit wait to that element with a condition.

Like this, there can be “n” number of conditions for which you can apply explicit wait, say, for example, wait till an alert is present, or wait till color property of a WebElement change and so on.

Refer the below screenshot for better illustration of different conditions in explicit wait.

Explicit Wait

                                                                                                          Explicit Wait

4. Fluent Wait-

In Fluent wait, a maximum time is defined to wait for a condition and along with that polling time is also defined. Polling time is the frequency with which the condition is checked. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

Let us look at the code:

In this code, the maximum time to wait is passed as timeout, polling time (frequency with which condition is checked again) is passed as pollingTime and we are waiting for a condition to get true i.e. visibility of Element located by.

Note: To test Implicit, Explicit and Fluent wait try next scenario which is “Mouse Hover” operation without any timeout and then see the difference with timeouts.

For any questions, queries or comments. Feel free to write us at amrita@qatechhub.com or support@qatechhub.com. Happy learning 🙂

Amrita Joshi

About the Author

Amrita Joshi

Leave a Comment: