Making WAIT Independent of DOM Element in Selenium


Problem:- While working on automation of, I faced a common problem to wait for entire page to successfully load in browser.

Available solution in Selenium .
Wait in selenium is used when an element is not visible on page within the time limit and it shows NoSuchElement found or Timeout Error exceptions. This problem Often comes on a slow network where the speed of Selenium driver has not matched with the speed of browser.

To overcome this problem there are two main type of wait in Webdriver.

1. Explicit Wait
2. Implicit Wait

Explicit Wait:- An explicit wait lets you define to wait for a certain condition to occur before proceeding further. In explicit wait, it starts looking for the element for the specific time period and return true if element is visible and false if it is not, due to this execution of test becomes fast.

Java Sample Code:-
public static void explicitWaitUsingWebDriver(final String S){
Wait wait = new WebDriverWait(driver, 10);
// Wait for search to complete
wait.until(new ExpectedCondition() {
public Boolean apply(WebDriver webDriver) {"Searching...");
return webDriver.findElement(By.xpath(S)) != null;

This waits up to 10 seconds trying to find the element with a default time interval of 500ms.

Implicit Wait:- Implicit wait is used when elements are not immediately available and it informs the webDriver to poll the DOM for a certain amount of time, when trying to find an element. Due to this process execution of test becomes slow. The default setting for implicit wait is zero. Once set, the implicit wait is set for the life of the WebDriver object instance. Since its an interface, so have a function named as implicitlyWait(). This method takes the argument of time in seconds. Sample code is written below:-

WebDriver driver = new FirefoxDriver();

The problem in using above types of Waits are :-
1. They are dependent on some DOM element.
2. Implicit wait is set for the life of webdriver object instance.

Solution of the Problem(Making DOM element independent of Wait):-
By using the above type of waits in selenium, still some time execution of code is not as fast as expected. These days all sites are using AJAX functionality and while automating a AJAX application, a tester always gets confused about which element will be in expected wait condition. I faced the above condition for which solution needed to be “when the next line of code is found proceed only when the browser sends an acknowledgment”.

Here is a custom solution of the above problem.

All browsers have an inbuilt function “document.readyState” which returns status as “loading” while the document is getting loaded, and status “interactive” once it has finished parsing but still loading sub-resources, and status “complete” once it has loaded.

In the code given below, “rather than webdriver wait for an element, dependency is imposed on browser status to meet expected wait condition”.

DOM Element Independent Wait
public static void waitForPageLoaded(WebDriver driver) {
ExpectedCondition expectation = new ExpectedCondition(){
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) driver).executeScript(
"return document.readyState").equals("complete");
Wait<WebDriver> wait = new WebDriverWait(driver, 10);
try {
} catch (Throwable error) {
logger.error("Timeout waiting for Page Load Request to complete.");

For further reading, follow the links given below.


Leave a Reply

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

− six = 1

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>