I have a problem in finding element where page gets refreshed. Now trying to do anything on the element throws below StaleElementReferenceException
with a message Element is no longer valid
Looking up this url
there's a note on the above ref url about :
Should you wish to head down this route, the simplest hook point is to call setElementConverter
what or how is the setElementConverter
used ? googled up for a bit and couldn't find an actual implementation of the method mentioned.
I think you may be heading down the wrong path by focusing on setElementConverter
. As the documentation page you linked suggests, you should simply try to find the element again if it went stale.
If the element has been replaced with an identical one, a useful strategy is to look up the element again.
I think that if you are a beginner Selenium user, you should follow this advice and stop here. Try-Catch the stale element exception and just find the element again without worrying about setElementConverter
.
If you are looking into more advanced behavior of Selenium or are dead set in satisfying your curiosity into setElementConverter
, then the following lines will matter more.
If you do this automatically, be aware that you may well be opening your tests to a race condition and potential flakiness.
...
Should you wish to head down this route, the simplest hook point is to call setElementConverter.
The documentation is saying that you may try to write something clever in order to automatically repeat the find for the element, but that causes flakiness and race conditions. I don't think anybody in practice actually tries to overcome StaleElementExceptions this way because it's complicated and flaky, and the easiest solution is to re-find the element in your own code.
As @SantiBailors pointed out in his comment, setElementConverter is a protected
method in RemoteWebDriver
.
It looks like you would extend RemoteWebDriver and inject additional behavior into the "hook" of setElementConverter, or provide your own JsonToWebElementConverter
to change that behavior to automatically retry or deal with the stale element.
How you would do this, I'm not sure. This is where my knowledge ends, and I've never heard of anyone taking this advice to hook into setElementConverter
. Again, I'd like to reiterate that this probably isn't what you want to be doing, and most likely you just want to find the element again in your own code, which can be done so much more simply by using a try-catch for StaleElementException
and trying again after some ThreadSleep or WebDriverWait.
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