How to avoid “StaleElementReferenceException” in Selenium?

前端 未结 16 2341
一向
一向 2020-11-22 12:12

I\'m implementing a lot of Selenium tests using Java. Sometimes, my tests fail due to a StaleElementReferenceException. Could you suggest some approaches to mak

16条回答
  •  暖寄归人
    2020-11-22 12:48

    Kenny's solution is deprecated use this, i'm using actions class to double click but you can do anything.

    new FluentWait<>(driver).withTimeout(30, TimeUnit.SECONDS).pollingEvery(5, TimeUnit.SECONDS)
                        .ignoring(StaleElementReferenceException.class)
                        .until(new Function() {
    
                        @Override
                        public Object apply(Object arg0) {
                            WebElement e = driver.findelement(By.xpath(locatorKey));
                            Actions action = new Actions(driver);
                            action.moveToElement(e).doubleClick().perform();
                            return true;
                        }
                    });
    

提交回复
热议问题