Google Analytics


To search for specific articles you can use advanced Google features. Go to and enter "" before your search terms, e.g. CSS selectors

will search for "CSS selectors" but only on my site.

Thursday, October 31, 2013


In an older article I wrote about a method I created called waitForElement. After looking through some of the WebDriver code I found WebDriverWait. You should read the original article before you read this one.

The WebDriverWait class extends FluentWait<WebDriver> which is a special version of FluentWait which has WebDriver instances.

In the article for waitForElement I was talking about how Selenium cannot detect that Javascript has finished altering the DOM. So if you try to interact with an element with Selenium and Javascript is still altering it, you will have undefined results. The waitForElement method would wait for the element to be full rendered, i.e. for the Javascript to be finished. The WebDriverWait serves the same purpose.

To define a WebDriverWait would be the following:
WebDriverWait wdw = new WebDriverWait(driver, timeoutInSeconds, pollingTimeInMs);
The first parameter is an already existing WebDriver element. The second parameter is how long we want to wait for the element to be present. If we set it to say 30 it will wait for 30 seconds before it throws an error. The third parameter is how often you want to check for the element. If you set it really low, e.g. 10, it will check every 10 milliseconds. However this could put a load on the test machine. If you know it typically takes 55 milliseconds then maybe waiting for 60 would be best. You would not want to wait for 1000 milliseconds as it would make your test too slow.

This just creates the WebDriverWait object. It doesn't actually do the waiting. Let's say you click the ACCEPT checkbox. This causes Javascript to make the Next button visible. If you just click the ACCEPT checkbox then click the Next button it might fail because the Next button isn't visible as quickly as you can click it. So you need to click the ACCEPT checkbox, wait for the Next button to be visible then click the Next button. Here is some example code:
long timeoutInSeconds = 30;
long pollingTimeInMs = 250;
WebDriverWait wdw = new WebDriverWait(driver, timeoutInSeconds, pollingTimeInMs);
This will check every 250 milliseconds to see if the element located by id='next' is visible. If the element does not become visible in 30 seconds it will fail the step and throw an error.

The list of conditions you can wait for are:
elementSelectionStateToBe(by, true);
elementSelectionStateToBe(we, true);
textToBePresentInElement(by, text);
textToBePresentInElementValue(by, text);
The WebDriverWait also lets you alter the polling time, timeout, message displayed when it times out and exceptions it should ignore while waiting:
wdw.pollingEvery(delayBetweenPolling, TimeUnit.MILLISECONDS)
.withTimeout(timeoutInSeconds, TimeUnit.SECONDS)
Because you can set the unit of measure for polling time and timeout, you can refine these after you instantiate the WebDriverWait object.



GK said...

Hi Darrell,
The Blog is awesome.I have read the example and tried to use the same example in my scripts.
But I am getting the error in the scripts as
The method visibilityOfElementLocated
is not defined in the Script.Is it like I have to write the method for this or it is an inbuilt method.If it is a inbuilt method how can resolve this without any error.

Darrell said...

For the visibilityOfElementLocated to be resolved you need two things. The appropriate jar files need to be added to your project. If things like WebDriver, findElement, etc. are being resolved then you probably have the correct jar files.

The other thing is to have the correct import statements. For visibilityOfElementLocated you need:

import static;

to be used.

GK said...

Thanks a lot.I was able to solve my problem.Thanks a lot.