Selenium Webdriver - Wait for an element to load


: 5

I am currently working on a project that uses a lot of AJAX and delayed loading of HTML elements. This means that when a page loads, the object that I am looking for might not necessarily have appeared yet. In many instances the element may only be added to the DOM after an amount of time by the use of some JavaScript.

Selenium Webdriver

At first, we decided to write our automated tests as normal using Selenium Webdriver - but we soon noticed that this would be a bit trickier if the elements that we needed to test only appeared after the page loaded. After a bit of research, I came across Explicit and Implicit Waits in Selenium. In Selenium, waiting is having the automated task execution elapse a certain amount of time before continuing with the next step. There are two different kinds of waits - Explicit and Implicit Waits. An explicit wait is code you define to wait for a certain condition to occur before proceeding further in the code. An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.

Let's run through a quick example using both waits. In this example I am going to run through the search functionality on the homepage. This is a good example to use because the automplete functionality only appears after the user has typed in the text box. In this example we are going to test that the correct search results get returned when we enter certain keys.

In the code below we navigate to the website. Then we find the element named "ddRestaurants", click the element and enter the letters "or" in the textbox.

Unfortunately the above code will fail with the following error:

OpenQA.Selenium.NoSuchElementException : Unable to locate element: {"method":"class name","selector":"ac-row-110457"}

This is because we expect to see the autocomplete pop-up element appearing instantly and because the Selenium code is executing faster than the Ajax takes to complete it won't be able to find the element and it will throw an error.

Explicit wait

This is where the explicit wait starts to become really useful. Instead of instantly looking for an element, we can tell Webdriver to wait for a certain amount of time before we continue with the next step. The example below runs through the usage of an explicit wait. In order to use the wait functionality in Selenium Webdriver, you will need to add a reference to the Webdriver.Support.dll, as well as the standard Webdriver.dll. You can either download this directly from the Selenium website, or I like to use the Nuget packages and add it directly from the Solution Explorer in VS2010.

If you run this test, you will notice that it passes successfully because we have explicitly told it to wait for a certain amount of time before continuing.

Implicit Wait

Another option that is available to use is the implicit wait. The difference with the implicit wait is that it will tell Webdriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.

The example below runs through an implicit wait. Notice how it differs from an explicit wait.

One thing to keep in mind is that once the implicit wait is set - it will remain for the life of the WebDriver object instance. Personally I prefer to use the explicit wait because it only pauses before continuing, but you will need to decide on what suits your situation best. For more information on waits in Webdriver, please follow this link.

I have created a small sample with the tests used in this post. For more information - please download here.



tarun - 11/7/2011
So how many times do you end up in using explicit wait as I assume that you don't use implicit wait. I prefer implicit wait as, though my tests which expects unavailability of certain elements take longer because of implicit wait.

bose - 11/10/2011
Awesome logic, enhanced my skills

Ross Patterson - 1/9/2012
The most common criticism of Selenium RC tests is that the use of Thread.Sleep() is a bad idea (e.g., the timeout depends on the speed of the computer and network, plus it's just a guess). Now WebDriver comes along and turns that "bad idea" "good practice". :-(

Mike - 4/6/2012
Ross, this is not doing a sleep(), it is setting a time to wait before reporting a failure, which is completely different.

Atin - 6/26/2012
Can anyone please explain meaning of poll the DOM?

vamshi - 8/14/2012
How to Import IWebElement I mean which jar file consists of this IWebElement

Shwetha - 8/22/2012
In my case, the elements readily appear on load of the page, but they take time to align. During this time, the next statement (i.e. to enter value in a text box) is executed by the system, but the text isn't entered in real, and so the the test fails. What sort of a wait do I use i this case?

Tincho - 12/18/2012
@vamshi: IWebElement is in CSharp from .NET, in java is just WebElement @Atin: DOM is where all the page elements are declared for access or manipulation.

rstrainings - 4/15/2015
Thanks for posting ..

software testing training - 5/8/2015
Regarding for query features of TestNG and list some of the functionality in TestNG which makes it more effective? what is the difference between Implicit wait and Explicit wait?

megha - 10/27/2015
i have my automation scripts that run one after the other validating the functionalities of the screen. In my case I dont want to include explicit or implicit wait in each test case(to avaoid the overhead). Is it possible I can control the speed by just using wait once for all the test cases.

YO - 1/20/2016
Thank you very much. This codes very helpful.

Guy - 3/13/2016
This doesn't work. I think the Site updated it's code so the FindElements class names are no longer valid.

Ashish - 6/30/2016
We offer training in selenium with C# For course details refer: sharp-course-details For free videos on Selenium with C# refer sharp-tutorial-1

Unkown - 11/2/2016
I need to use this but through python, i have list(table) in webpage the table is very long consisting on multiple pages, in order to move to next part of table arrow type button is clicked i have to wait untill it loads data.., statement is; Browser.find_element(By.ID,value="next").click() i need to wait untill it loads new section of table how can i do this.??

VIVEK - 1/5/2018
An implicit wait tells WebDriver or Selenium Scripts to poll the DOM for a certain amount of time when Selenium Scripts trying to find an Object/Element or Object/Elements if they are not visible or Interactable. xplicit wait tell the WebDriver to wait for certain time on basis of certain Expected conditions before throwing an “ElementNotVisibleException” exception ,Explicit wait is specific wait applied only for specified elements. Explicit wait have better flexibility then Implicit wait. For Detail With Best Example Link :

Add your comment