线程“main”中的异常org.openqa.selenium.StaleElementReferenceException:缓存中不存在元素

时间:2013-03-09 10:24:55

标签: selenium-webdriver

当我尝试在循环中单击元素时,我收到StaleElementReferenceException。以下是代码:

WebDriver driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().window().maximize();

driver.navigate().to("URL");
WebElement Links=driver.findElement(By.xpath("//*[@id='chromemenu']"));
List <WebElement> AllLinks=Links.findElements(By.tagName("a"));
System.out.println(AllLinks.size());
//String start_xpath="//*[@id='chromemenu']/ul/li[";     
//String end_xpath="]/a";

for(int i=1;i<AllLinks.size();i++)
{
    System.out.println(AllLinks.get(i).getText());
    System.out.println(AllLinks.get(i).getAttribute("href"));
    //AllLinks.get(i).click();
        //This is where i am clicking  approach 1
    //driver.findElement(By.xpath(start_xpath+i+end_xpath)).click(); 
        //This is where i am clicking  approach 2
    //driver.navigate().back();
}

1 个答案:

答案 0 :(得分:0)

问题是您在循环列表时尝试单击链接。 您应该将链接保存在另一个元素中,并在完成循环后单击它以避免此错误