How to avoid “StaleElementReferenceException” in Selenium?

前端 未结 16 2288
一向
一向 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 13:01

    Try this

    while (true) { // loops forever until break
        try { // checks code for exceptions
            WebElement ele=
            (WebElement)wait.until(ExpectedConditions.elementToBeClickable((By.xpath(Xpath))));  
            break; // if no exceptions breaks out of loop
        } 
        catch (org.openqa.selenium.StaleElementReferenceException e1) { 
            Thread.sleep(3000); // you can set your value here maybe 2 secs
            continue; // continues to loop if exception is found
        }
    }
    

提交回复
热议问题