How to deal with StaleElementReferenceException in Selenium Webdriver
What causes StaleElementException?
1)Page Refresh : A page may look like one page but in reality it may be different pages, so you might have to refind them to work with it
2)JavaScript MVC Frameworks : Many JavaScript MVC Frameworks can update the contents by rapidly tearing down and rebuilding the same elements
Example : Share prices and Graphs during market hours
4)Elements being moved : If elements disapperas of the screen and it is to be relocated, the old refrence goes stale(graphs in moneycontrol.com)
5)Elements being re-rendered : If you are working on portal, some portsal may get updated continiuosly
How to handle it ?
1)Instead of directly storing the locators, use a method to reference the element
2)Proactively wait for the element to go stale, for this you can keep a counter, say, you have button or entry when clicked or updated increment the counter(1) and re-find the element and drcrement the counter(0)
In this way, when ever you get stale element exception, stop everything, put a counter and handle it right then, This will definetly reduce the frequency of exception
This technique did work for me.
3)Use more javascript executor for such elements, not so good way to habdle but very effective
You can also refer to the below links, It has some good exmples to handle such suitation in python
https://github.com/wiredrive/wtframework/blob/master/wtframework/wtf/utils/wait_utils.py
https://github.com/dlai0001/selenium-tech-demo1
What causes StaleElementException?
1)Page Refresh : A page may look like one page but in reality it may be different pages, so you might have to refind them to work with it
2)JavaScript MVC Frameworks : Many JavaScript MVC Frameworks can update the contents by rapidly tearing down and rebuilding the same elements
Example : Share prices and Graphs during market hours
4)Elements being moved : If elements disapperas of the screen and it is to be relocated, the old refrence goes stale(graphs in moneycontrol.com)
5)Elements being re-rendered : If you are working on portal, some portsal may get updated continiuosly
How to handle it ?
1)Instead of directly storing the locators, use a method to reference the element
2)Proactively wait for the element to go stale, for this you can keep a counter, say, you have button or entry when clicked or updated increment the counter(1) and re-find the element and drcrement the counter(0)
In this way, when ever you get stale element exception, stop everything, put a counter and handle it right then, This will definetly reduce the frequency of exception
This technique did work for me.
3)Use more javascript executor for such elements, not so good way to habdle but very effective
You can also refer to the below links, It has some good exmples to handle such suitation in python
https://github.com/wiredrive/wtframework/blob/master/wtframework/wtf/utils/wait_utils.py
https://github.com/dlai0001/selenium-tech-demo1
No comments:
Post a Comment