简体   繁体   中英

Selenium: StaleElementReferenceException

I've been researching this error for a while and have tried many things and nothing seems to work...

        while(!driver.findElements(By.className("next")).isEmpty()) { 
            //elements = driver.findElements(By.xpath("//a[@class='name']"));
            elements = findDynamicElements("//a[@class='name']");
            for (WebElement e : elements) {
                userName = e.getText(); //<--EXCEPTION HERE
                check_visitor_profile(userName);//<--WE LEAVE THE PAGE HERE
                Thread.sleep(3000); //<--NO TRY/CATCH BLOCK FOR READABILITY
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                elements = findDynamicElements("//a[@class='name']");

            }
            driver.findElement(By.xpath("VisitsNext")).click();
        }       



protected List<WebElement> findDynamicElements(String path) {
        List<WebElement> result;
        String xPath = path;
        new WebDriverWait(driver, 25).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(xPath)));
        //new WebDriverWait(driver, 25).until(elementIdentified(By.id(path)));

        try {
            result = driver.findElements(By.xpath(xPath));
            return result;
        }
        catch(WebDriverException e) {
            return null;
        }
    );
    }

My code craps out on the first line of the for loop where userName is assigned. I've seen on this forum that you should use 'presenceOfElementLocated' and explicitly wait for the element to come back but that doesn't work either. I used 'presenceOfAllElementsLocatedBy' for a list but I have a method that uses 'presenceOfElementLocated' which doesn't work either.

I know stuff like the Thread.sleep and the implicitlyWait line is probably unnecessary at this point but I've literally tried everything and it doesn't work...

The error occurs because when I call 'check_visitor_profile' it leaves the page - when it comes back the elements are out of place so I have to find them again. Which I do but it still throws the exception.

Any Ideas?

Thanks.

The problem might occur because you are changing elements in the middle of the loop. It will cause you trouble even without the StaleElementReferenceException . Use a for loop instead of the for each loop

elements = findDynamicElements("//a[@class='name']");
int size = elements.size();
for (int i = 0 ; i < size ; ++i) {
    elements = findDynamicElements("//a[@class='name']");
    userName = elements.get(i).getText();
    check_visitor_profile(userName);
}

Handle the exception explicitly as the element is no longer attached to the DOM or has changed at that moment you call "check_visitor_profile"

See the link below might help

            catch(StateElementException e){
             System.out.println("StaleElement dealt with since you successfully left page "); 
              }

http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM