Selenium: How to avoid StaleElementReferenceException when looping through a set of elements?

前端 未结 2 908
野的像风
野的像风 2020-12-21 13:33

I have a page that contains a bunch of tables. I loop through the tables in the outer loop and then loop through each row in the table in the inner loop. It all works fine

2条回答
  •  庸人自扰
    2020-12-21 14:14

    Well after tearing my hair out for a day, I finally realized what was happening. It should have been obvious to me. When the "Next" button is clicked, it takes some time for the new page to load. By simply adding a delay, the new DOM is loaded and processing begins on it, not again on the previous one!

                driver.findElement(By.xpath(".//*[@class='PageRight']")).click();
            try {
                Thread.sleep(4000); //provide some time for the page to load before processing it
            } catch (InterruptedException ex) {
                Logger.getLogger(RealAuction.class.getName()).log(Level.SEVERE, null, ex);
            }
    

    Now it runs to completion with no StaleElementReferenceException.

提交回复
热议问题