Handling Selenium Wait for Page Reloads After Modal Close

How do I ensure Selenium waits until a page completely loads?

I’m automating test cases using Java and Selenium WebDriver. Here’s the scenario:

I navigate to a page named ‘Products’. On this page, when I click the ‘View Details’ link for a product, a popup (modal-dialog) opens displaying item details. After closing the popup by clicking the ‘Close’ button, the page refreshes automatically (the content remains the same). Post-refresh, I need to click an ‘Add Item’ button on the same page. The challenge is:

If the internet speed is fast, WebDriver finds and clicks the ‘Add Item’ button before the page refreshes, causing a StaleElementReferenceException. Using different wait conditions doesn’t reliably wait until the page has finished reloading because the content remains unchanged before and after the refresh. The test case works when I use Thread.sleep(3000); before clicking the ‘Add Item’ button. Is there a better solution or workaround for this issue?

I’ve been working with Selenium for over 5 years, and I’ve found several effective strategies to ensure Selenium waits until the page loads completely. Here are three approaches you can combine for better results:

  1. Set Implicit Wait: Start by setting an implicit wait right after creating the WebDriver instance. This tells WebDriver to wait for a specified time for elements to appear before throwing an exception.

    driver.manage().timeouts().implicitlyWait(timeoutInSeconds, TimeUnit.SECONDS);
    

    This wait is applied on every page navigation or reload.

  2. Wait for Document Ready State: After navigating to a new page or performing actions that trigger a page reload, use WebDriverWait to wait until the document’s readyState is "complete". This ensures that the entire DOM has finished loading.

    new WebDriverWait(driver, timeoutInSeconds).until(
        webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
    

    This approach waits dynamically until the page is fully loaded before proceeding.

  3. Check URL Pattern: Optionally, verify if the URL matches an expected pattern before interacting with elements on the page. This helps ensure that the page has navigated or reloaded as expected.

    WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
    wait.until(ExpectedConditions.urlMatches(expectedPattern));
    

    Combining these methods provides a robust way for Selenium to wait until the page is completely loaded, reducing the likelihood of StaleElementReferenceException and other timing issues.

In my 6 years of working with Selenium, I’ve encountered similar challenges. Here’s a solution to handle scenarios where you need to wait for a page to reload before clicking the ‘Add Item’ button:

  1. Wait for the ‘Add Item’ Element to Become Stale: You can wait for the ‘Add Item’ element to become stale and then interact with the reloaded element. Here’s a code snippet to achieve this:

    WebDriverWait wait = new WebDriverWait(driver, 20);
    By addItemLocator = By.xpath("//input[.='Add Item']");
    
    // Step 1: Wait for the 'Add Item' element to be present
    WebElement addItemElement = wait.until(ExpectedConditions.presenceOfElementLocated(addItemLocator));
    
    // Step 2: Trigger the page reload (replace '...' with the actual element locator or action that triggers the reload)
    driver.findElement(By.id("...")).click();
    
    // Step 3: Wait for the original 'Add Item' element to become stale
    wait.until(ExpectedConditions.stalenessOf(addItemElement));
    
    // Step 4: Wait for the reloaded 'Add Item' element to be present again and click it
    wait.until(ExpectedConditions.presenceOfElementLocated(addItemLocator)).click();
    

    In this example:

    • Step 1: Wait for the ‘Add Item’ element to be present before proceeding.
    • Step 2: Trigger the page reload with the appropriate action.
    • Step 3: Wait for the original ‘Add Item’ element to become stale, indicating the page has started reloading.
    • Step 4: Wait for the reloaded ‘Add Item’ element to be present again and click it.

    This approach ensures Selenium waits until the page loads completely, reducing the likelihood of encountering StaleElementReferenceException.

Having worked with Selenium for nearly a decade, I’ve come across various methods to handle delays effectively. Here are two commonly used approaches to ensure Selenium waits until the page loads:

  1. Implicit Wait: This method sets a global timeout for the WebDriver to wait for elements to appear on the page before throwing an exception.

    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    

    This approach is effective for scenarios where elements may take some time to load due to network latency or dynamic content changes.

  2. Try-Catch Method: Alternatively, handle delays using a try-catch block. This method involves explicitly waiting for specific conditions or handling exceptions when they occur, providing more control over how delays are managed in your script.

    try {
        // Code to interact with the element
    } catch (StaleElementReferenceException e) {
        // Code to handle the exception
    }
    

    This can be particularly useful for managing specific timing issues or exceptions like StaleElementReferenceException dynamically.

    Both approaches have their advantages depending on the complexity of your test scenarios and the level of control you need over waiting times and exceptions in your Selenium scripts.