End of Support for Microsoft Products: What You Need to Know
Stay informed about the end of support for Microsoft Office 2016 & 2019, Exchange Server 2016 & 2019, and Windows 10.
Avenga code experts are big fans of sharing their experience with technologies and sophisticated solutions.
If you are reading this you must have encountered Stale Element.
As you may already know a ‘Stale Element’ means == an old, outdated element…
An element can become outdated if a page is changed somewhere in between element declaration and actual use of element. (changes are usually caused by JavaScript, Ajax,JQuery etc.)
A common scenario looks like this:
Element declaration => Actions that cause changes(JS,Ajax etc.) => use of element => Stale Element exception.
The simple example is:
There are two possible ways to get Stale Element exception:
In first case: JS library or Ajax replaced an element with another one with the same ID or attributes. This element may look identical but it is different; the driver does not know if the replaced element is actually what’s expected and will give a Stale Element exception.
A common technique in a web app is to prepare DIVs for each tab, but only attach one at a time. Your code might have a reference to an element that is no longer attached to the DOM (that is, that has an ancestor which is “document.documentElement”).
There are many ways of dealing with Stale Element Exception on the web. Here I gathered the ones that I personally find most useful.
Example: @FindBy(xpath=”
someXpath”)
public WebElement someElement;
This method waits for all jQuery to finish.
public void waitForAjax() {
This method waits for page to be loaded.
More common practice is to use try catch in a loop.
NOTE: This approach is not very flexible and should only be used if ‘Stale Element’ cannot be prevented by other means. Also we should always put a system.out message into catch block, this will spare you few hours if you get an error anyway.
If you have a piece of code similar to this one:
Try changing it to a format below. Doing so your element will be declared directly before actual usage.
Hope this information was helpful. Try preventing the problem so you won’t have to fix it later!
Ready to innovate your business?
We are! Let’s kick-off our journey to success!