Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is setElementConverter and how does it work in Selenium WebDriver?

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.

like image 592
Bandeesh R Sirga Avatar asked Jun 04 '14 05:06

Bandeesh R Sirga


1 Answers

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.

  • Selenium API Documentation for RemoteWebDriver setElementConverter
  • Selenium source code on GitHub for RemoteWebDriver setElementConverter
    • JsonToWebElementConverter Class in the source code

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.

like image 95
George Pantazes Avatar answered Nov 12 '22 18:11

George Pantazes