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!
I see 3 ways to handle your issue.
The first solution and probably the most reliable would be to find the elements once the page has reached a stable state. It could be done either by waiting for the presence of an element or by waiting for the count of elements.
A second solution would be to list again the elements when StableElementReferenceException is raised :
List<String> headers = new ArrayList<String>();
List<WebElement> elements = findAllByXpath(path);
for(int i = 0; i < elements.size(); ) {
try {
headers.add(elements.get(i).getText());
i++;
} catch (StableElementReferenceException ex) {
elements = findAllByXpath(path);
}
}
return headers;
And a third solution would be to get the text for all the elements with a piece of Javascript:
List<WebElement> elements = findAllByXpath(path);
ArrayList headers = (ArrayList)((JavascriptExecutor)driver).executeScript(
"var arr = [], elements = arguments[0];" +
"for(var i=0; i<elements.length; i++)" +
" arr.push(elements[i].textContent);" +
"return arr;", elements);
return headers;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With