Selenium: Wait for Page to load

Handling the page loading times for web automation can be really frustrating. Especially when you think your scripts are all 100% working until you run one and it fails on a NoSuchElement exception. A lot of web applications now are very ajax heavy meaning even when selenium detects that your element is present  and loaded there are still parts of the application loading. You can test this by executing a screenshot after your findElement code and you’ll see that the page is not totally loaded. You can find numerous solutions online but several doesn’t quite do the trick. There is one however that worked for my case and it can be found at Brantley’s blog.

The method works because it checks for three things. First, it checks if the normal load page is complete. Then it will wait for if any ajax requests to load and lastly it will check again for the normal page to complete. The code used on my case can be found below. All credit belongs to the original poster in the link above, the code has been tweaked a little to work on my particular project .

To call the whole thing which is located in a class called Wait.


Wait.WaitForReady(driver, TimeSpan.FromSeconds(120));

In the Wait class, a wait until is called.

public void WaitForReady(IWebDriver driver, TimeSpan seconds)
{
    WebDriverWait wait = new WebDriverWait(driver, seconds);
    wait.Until(d =>
    {
        return WaitForPageLoad(driver, seconds);
    });
}

A Method that will do three checks.

public static bool WaitForPageLoad(IWebDriver driver, TimeSpan waitTime)
        {
            WaitForDocumentReady(driver, waitTime);
            bool ajaxReady = WaitForAjaxReady(driver, waitTime);
            WaitForDocumentReady(driver, waitTime);
            return ajaxReady;
        }

The method for page load.

private static void WaitForDocumentReady(IWebDriver driver, TimeSpan waitTime)
        {
            var wait = new WebDriverWait(driver, waitTime);
            var javascript = driver as IJavaScriptExecutor;
            if (javascript == null)
                throw new ArgumentException("driver", "Driver must support javascript execution");

            wait.Until((d) =>
            {
                try
                {
                    string readyState = javascript.ExecuteScript(
                        "if (document.readyState) return document.readyState;").ToString();
                    return readyState.ToLower() == "complete";
                }
                catch (InvalidOperationException e)
                {
                    return e.Message.ToLower().Contains("unable to get browser");
                }
                catch (WebDriverException e)
                {
                    return e.Message.ToLower().Contains("unable to connect");
                }
                catch (Exception)
                {
                    return false;
                }
            });
        }

A method that will wait for (if any) Ajax requests.

private static bool WaitForAjaxReady(IWebDriver driver, TimeSpan waitTime)
        {
            System.Threading.Thread.Sleep(1000);
            WebDriverWait wait = new WebDriverWait(driver, waitTime);
            return wait.Until<bool>((d) =>
            {
                return driver.FindElements(By.CssSelector(".waiting, .tb-loading")).Count == 0;
            });
        }

This greatly helped in script stability for my c# projects. I managed to also write this in java which I will do a later post on.

Author: Philip Caande

Philip is a 30 year old amateur technical blogger. He loves anything about automation testing and loves wine with friends. His free time consists of basketball, badminton, jogging, eating good food, watching movies, solving puzzles and algorithm programming problems.

One thought on “Selenium: Wait for Page to load”

Leave a comment