wait for page load in selenium

Wait for Page Load in Selenium

Last updated on

In this tutorial, we will learn how to wait for a page to load in Selenium before interacting with the web elements present on that page in order to avoid NoSuchElementException.

Before going further into the implementation of wait till page load in Selenium you need to understand the following points-

  • An element that triggers page load waits until the DOM gets loaded before returning control to the driver.
    For example – if we submit a form by clicking the Submit button then the next statement after the submit button click operation will be attempted by WebDriver only after the page gets loaded completely. So, in most cases, we don’t even have to wait for the page to load.

  • We may have to introduce a wait time when dealing with elements that are ajax based or dynamic elements that load even after the page load.

  • In certain cases, we deal with elements that may be visible on page load or after some trigger action but take some time to be available for interaction.
    For example, a dropdown with dynamic values may be available on the DOM throughout but have its values populated only after we perform some action based on which the corresponding values get dynamically populated.
    So, in this case, if we try to select a particular value, we must wait for some time for the element to be available for interaction.

Wait for the page to load Implementation

Selenium Webdriver doesn’t provide any inherent support for wait till page load implementation. But we can make use of Explicit Waits to achieve the desired outcome. For this, we need to identify the element on the webpage that’s the last one to load or become available for interaction.

Now, we need to use the explicit wait with the appropriate Expected condition like “ElementToBeClickable” for page load implementation. Thus, making sure that the driver waits till all the web elements get loaded successfully before attempting to interact with them.

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(lastElementToLoad));

5 thoughts on “Wait for Page Load in Selenium”

  1. Hi,
    How to convert your example to python?
    I’m testing a site that uses ajax, and I want to know when it is fully loaded, hence i would like to wait for the last element to be clickable as your example shows.
    But, it python i don’t find a parameter like “lastElementToLoad”, as much as i know.
    (Python:
    Parameters: locator – It is a tuple of (by, path)
    example: element = wait.until(ec.element_to_be_clickable((By.ID,’disabled-register-button’)))
    )

    I would be glad to receive your advice.

    Regards,
    Sarai

    Reply
  2. I am writing a script ,in which i need to Get the Row count of the Table,which has more than 500 rows,In this case I am using Explicit Wait,But as soon as First Row is Loaded,script is not waiting ,its just going with Further Execution,How can i achieve this Test case.

    WebDriverWait wait = new WebDriverWait(driver, 70); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//div[@id=’kendoGridTransactionAndApprovals’]”)));

    Reply
    • You need to think of some other strategy or locator. As the script would continue as soon as the element becomes visible i.e. as soon as the first row is loaded.

      Reply
  3. You’ll need to create an ExpectedCondition that uses JS to wait for complete status of DOM. Then use that expected condition in WebDriverWait.

    Reply

Leave a Comment