Wait in Selenium WebDriver

September 21, 2016

While working on any Automation project, the biggest challenge you will face is Synchronisation ie. Syncing up the scripts with WebElements. 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 which will help in syncing up the script with WebElements.

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

  1. PageLoadTimeout – This is the maximum time selenium waits for a page to get 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. So, here in the above example , WebDriver will wait for maximum 90 seconds till the page gets loaded.If it gets 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 Page not found 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 – This 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 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 say, for example, wait till an alert is present, or wait till colour property of a WebElement changes and so on.
Refer the below screenshot for better illustration of an explicit wait.

Explicit Wait

                                                                                                          Explicit Wait

Note: To test Implicit and Explicit 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: