20

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.

George Pantazes
  • 1,039
  • 3
  • 12
  • 26
Bandeesh R Sirga
  • 221
  • 1
  • 3
  • 6
  • 1
    Can you give a code snippet which does not work? – Priyanshu Mar 16 '15 at 07:52
  • 1
    Same here, cannot find info about `setElementConverter`, even in the doc itself and in the source code. And `setElementConverter` (from class `RemoteWebDriver`) is `protected`, so in their cryptic "Should you wish..." hint they also seem to be implying that their "down this route" involves extending `RemoteWebDriver` and calling `setElementConverter` from somewhere in the subclass. – SantiBailors Feb 07 '18 at 09:27
  • @bandeesh-r-sirga In case this is an XY question and you're actually just trying to get things working with Selenium, you need to re-find the element once it's stale. If you get a StaleElementException, driver.findElement(By) again and the element will no longer be stale. Elements go "stale" if the DOM tree changes at that node or above it in the hierarchy. Would finding the element again suit your needs, or do you absolutely have to deal with the same stale element that you originally found? – George Pantazes Nov 07 '18 at 23:23

2 Answers2

1

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.

George Pantazes
  • 1,039
  • 3
  • 12
  • 26
0

You can't directly access setElementConverter method of RemoteWebDriver as it is protected. It can be used by extending webdriver.

One of the example of how the setElementConverter can be used, is in QAFExtendedWebDriver of QAF where QAF has extended webdriver and webelement to have additional features like listeners, inbuilt assertion, verification, wait, self-descriptive locator and so on.

user861594
  • 5,733
  • 3
  • 29
  • 45