Fix Stale Element References: AJAX Table Loading (Katalon)

stale element references ajax table loading katalon studio

Stale element references are common when AJAX tables reload. Learn how to handle them in Katalon Studio with SmartWait, exception handling, JavaScript load checks, and retry logic, with code examples.

Key takeaways
The 30-second version of this article
Stale element references happen when an element is removed or re-rendered in the DOM before your test acts on it, which is common with AJAX tables.
Enable SmartWait in Katalon Studio so tests wait for the page to be ready before acting.
Catch StaleElementReferenceException and re-locate the element instead of letting the test fail.
Use JavaScript checks, such as jQuery.active on jQuery pages, to confirm AJAX calls have finished.
Add bounded retry logic with explicit waits for highly dynamic DOMs.

The information in this blog post is based on a real-life scenario shared by a user on our Katalon Community forum and is intended to inspire peer-to-peer discussion and collaboration. Please always test solutions thoroughly before implementing them in a production environment.

Join in the discussion here.

Introduction

Navigating dynamic web applications often introduces challenges like handling stale element references, especially when dealing with AJAX table loading. This common issue can disrupt your automated testing flow, but Katalon Studio offers reliable solutions to streamline the process. In this post, we’ll explore effective strategies to manage stale element references and ensure your test automation remains reliable.

Understanding Stale Element References

Stale element references occur when an element previously located on the DOM is either removed or re-rendered before an action can be performed on it. This is particularly common with AJAX-based tables where the content is dynamically loaded or updated.

Using Katalon Studio’s SmartWait

One of the standout features of Katalon Studio is SmartWait, which intelligently waits for the web page to be in a ready state before executing actions. This feature significantly reduces the likelihood of encountering stale element references.

How to Enable SmartWait?

  1. Navigate to Project > Settings > Execution > Default > Web UI.
     
  2. Check the box for Enable Smart Wait.
     
  3. Configure the timeout settings as needed.

Exception Handling Techniques

Proper exception handling is important for dealing with unpredictable changes in the DOM. By catching StaleElementReferenceException, you can implement a retry mechanism or alternative actions to recover from the error.

Sample Code

try {
    WebElement element = driver.findElement(By.id("example"));
    element.click();
} catch (StaleElementReferenceException e) {
    // Re-locate the element, then retry or take alternative action
}

Using JavaScript for More Reliable Interaction

JavaScript can be a useful tool for interacting with dynamic elements. By using JavaScript to check the status of AJAX calls, you can make sure the table has finished loading before interacting with it.

Example Using jQuery

// In Katalon Studio: WebDriver driver = DriverFactory.getWebDriver()
JavascriptExecutor js = (JavascriptExecutor) driver;
// Only works on pages that load jQuery
Boolean ajaxComplete = (Boolean) js.executeScript("return window.jQuery != null && jQuery.active == 0");
if (ajaxComplete) {
    // Interact with the table
}

Implementing Retry Logic

In cases where the DOM is highly dynamic, implementing retry logic can help stabilize your tests. This approach re-locates the element and retries the action a limited number of times if it goes stale between lookup and interaction. (In Selenium 4, WebDriverWait takes a Duration rather than a number of seconds.)

Example

int attempts = 0;
while (attempts < 3) {
    try {
        WebElement element = new WebDriverWait(driver, Duration.ofSeconds(10))
                .until(ExpectedConditions.elementToBeClickable(By.id("example")));
        element.click();
        break; // success
    } catch (StaleElementReferenceException e) {
        attempts++; // the table re-rendered; locate the element again
    }
}

Conclusion

Handling stale element references in AJAX table loading can be challenging, but with Katalon Studio’s features and best practices, you can maintain stable, reliable test automation. Enable SmartWait, use exception handling, use JavaScript checks, and implement retry logic to tackle this issue effectively.

If you’re facing a similar challenge, why not give these solutions a try? And remember, our community forum is a great place to share your experiences and learn from others. If you have any questions or need further assistance, don’t hesitate to Ask the Community!

Frequently asked questions

Son Huynh
Son HuynhSupport Engineer

Son Huynh is a Support Engineer at Katalon, based in Ho Chi Minh City, where he helps customers solve technical issues across the Katalon platform. Before joining Katalon in 2025, he spent three years as a Professional Services Engineer at ELCA Vietnam. He studied at California State Polytechnic University, Pomona.

Connect on LinkedIn →
Get started

Test everything. Ship with confidence.

From generating test cases to spotting failures, Katalon's AI works across your entire pipeline, automatically.

Live webinar

The Next Evolution of Manual Testing: Introducing Katalon AI Test Runner

Learn how to grow your QA team's capacity in the age of AI, then watch Katalon AI Test Runner turn manual test cases into automated runs, live.

2:00 to 3:00 PM IST
Almas AhmedSenior Account Manager
Sumit WankhedeSolutions Engineer Manager
Register now