Stale Object Reference while Navigation using Selenium

后端 未结 3 1120
攒了一身酷
攒了一身酷 2021-01-26 12:16

I have been trying a simple program that navigates and fetches data from the new page, comes back in history and open other page and fetch data and so on until all the links hav

3条回答
  •  悲&欢浪女
    2021-01-26 12:35

    The reason you get StaleElementReference Exception, is normally because you stored element(s) into some variable, however after that you did some action and page has changed (due to some ajax response) and so your stored element has become stale.

    The best solution is not to store element in any variable in such case.

    This should work.

    links = driver.findElements(By.xpath("//*[@id='gvSearchResults']/tbody/tr/td[1]/a"));
    
    for (int j = 0; j < links.size(); j++) {
        System.out.println(links.get(j).getText() + ", ");
        driver.findElements(By.xpath("//*[@id='gvSearchResults']/tbody/tr/td[1]/a")).get(j).click();
        System.out.println("Afte click");
        driver.findElement(By.id("ctl00_MainContent_btnBack")).click();
        this.search();
    }
    

提交回复
热议问题