0

With the most recent update on chrome browser and chromedriver I've started getting staleElementReferenceException in WebDriver.
This should have been handled by my custom Wait method, but in some (rare cases) it still happens. Here is my wait method:

public static WebElement getLoadedElement(String selector){
    final Wait<WebDriver> longWait =
        new FluentWait<WebDriver>(Driver.getDriver()).withTimeout(15, TimeUnit.SECONDS)
            .pollingEvery(1, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class)
            .ignoring(StaleElementReferenceException.class);
        final WebElement element = longWait.until(new Function<WebDriver, WebElement>() {
            @Override
            public WebElement apply(final WebDriver driver) {
                return driver.findElement(By.xpath(selector));
            }
        });
    return element;
}  

Is there another way of doing this without having to add a wrapper around all of the basic WebDriver actions (such as .click(), .getText(), etc.) ?

The only other solution I've found is by creating a counter and attempting to find the element over and over again at set intervals (0.5 seconds) but for this I'm having trouble catching multiple exceptions at the end of the timeout(counter).

Checking for .isClickable() is also not an option since I don't want to have to click on some elements in order to find them.

Any other ideas? I'm sure someone knows a better solution!

Sorin D.
  • 91
  • 3
  • 12
  • Wrapping every basic action is indeed tedious, but have you considered wrapping at a higher layer, for example, try/catch more complex functionalities (ones that consist of several such basic actions)? – goodvibration Nov 23 '17 at 13:26
  • @goodvibration Yes, I've considered, but that, again is very tedious, and the way I see things is that I need to re-try the actual actions, such as Click, and not simply to try and re-find the WebElement. – Sorin D. Nov 23 '17 at 13:30
  • @SorinD. Have you tried with visibility expectedcondition? You could try with implicit wait with a suitable timeout. Though this might mess up any existing explicit waits. – Grasshopper Nov 23 '17 at 13:52
  • @Grasshopper Good suggestion, I have tried with .isDisplayed() and it doesn't really help. The only implicit wait I have is 1 second before I interact with the element and I wouldn't like to add more than that. Thinking about it, maybe I should move this 1 sec before the actual find. I'll go try some stuff, thanks! – Sorin D. Nov 23 '17 at 14:03

0 Answers0