I’m writing Java Selenium tests for listviews, specifically ones to validate and then select an item from a listview.
It’s driving me crazy, though, as the tests will run fine half the time, but the other half they’ll give StaleElementReference or similar errors, often occuring at different stages each time. This is when running them under identical conditions.
I re-assign the WebElement variables each time the listview page changes as I gathered this might be the issue, but it still happens. I then thought it might be to do with needing to wait for the page to fully load but I’m not entirely sure how to make use of things like WebDriverWait that I’ve seen some others suggesting.
I do however know that our existing base page object class already includes something for waiting, so I think this may already be covered:
public abstract class PageObject {
protected WebDriver driver;
protected WebDriverWait wait;
...
public PageObject(WebDriver driver, String relativePath) {
...
this.driver = driver;
this.wait = new WebDriverWait(driver, 8);
...
Is that enough, or do I need a more specific form of ‘wait’ for this problem?
Aside from that, can anybody think of any possible common issues that might be causing this?
Thanks a lot
heh, come across with similar issue. The matter is when you got a lot of AJAX on the page, the best way (imho) to handle it and the wait i like most is
fluentWait()So you actually need to find the locator of web element (xpath, or css selector) and to pass it to fluent wait. And fluent wait, in turn, returns you found web element.
From the documentation:
An implementation of the Wait interface that may have its timeout and polling interval configured on the fly.
Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.
usage:
you can get more info on it here
The issue causing such kind of problem can be (from my experience ) of two types:
1) first one
you interact with an element causing page rendering or some else ‘slow’ operations. Selenium tests keep on going but the page actually not rendered >>
staleElement.2) second one.
you interact with one element. then you interact with another causing a lot of AJAX (not complete page reload, and first element refresh). and instantly you return back to the previous one (wanna e.g. getText or click)>>
staleElement.So I always use fluent wait. It is comfortable from the point you can set up arbitratry exceptions ignoring.
Hope this works for you.