StableElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up

Viewed 520

My code is like as following, the code is to get the elements set via xpath, and generate a list<String> with the texts of each element in the elements set. But it always throws exception during the process of adding each element's text to the new List<String>.

protected List<String> getHeaders(String path){
    List<String> headers = new ArrayList<String>();
    int count = 0;
    List<WebElement> elements = findAllByXpath(path);
    LOG.info("The length is : " + String.valueOf(elements.size()));

    for (WebElement c: elements) {
        LOG.info("->> " + String.valueOf(count));
        count++;
        headers.add(c.getText());
    }
    return headers;
}

The result is:

INFO com.xxx.test - The length is: 13
INFO com.xxx.test ->> 0
INFO com.xxx.test ->> 1
INFO com.xxx.test ->> 2
INFO com.xxx.test ->> 3
INFO com.xxx.test ->> 4
INFO com.xxx.test ->> 5

com.xxx.test -> getHeaders FAILED org.openqa.selenium.StableElementReferenceException:Element not found in the cache - perhaps the page has changed since it was looked up

Can someone help in this case? Thanks!

3 Answers
Related