Search code examples
htmlelements

Is it possible to refresh stale elements?


I have a search page that contains a table that gets populated with search results after the user presses the search button. The page object wraps the rows of the search results table in a custom HtmlElement class.

I'm getting a stale element exception when accessing the results table - as you'd expect because it was just refreshed by an ajax request.

I've worked around it by returning a new instance of the page object after performing the search but I'd rather just recreate the search results field. Is there any way to do this?

Example:

@FindBy(css = "[data-viewid='Table1'] .dojoxGridRow")
List<ActivityPerformanceRow> results;

// ...

public void search() {
    search.click()
    waitForAjaxToComplete();

    // If it was a standard WebElement list I'd do something like this:
    results = driver.findElements(By.cssSelector(
                      "[data-viewid='Table1'] .dojoxGridRow"));
}

Solution

  • After a bit of playing around I came up with this solution - it works well but doesn't deal with HtmlElement name property. Given I don't use it that I'm aware of I'm ignoring it for now...

    public <T extends HtmlElement> List<T> findElements(Class<T> elementClass, By by) {
        List<T> elements = new LinkedList<T>();
    
        for (WebElement element : driver.findElements(by)) {
            elements.add(HtmlElementLoader.createHtmlElement(elementClass, element, null));
        }
    
        return elements;
    }