当我尝试在循环中单击元素时,我收到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();
}
答案 0 :(得分:0)
问题是您在循环列表时尝试单击链接。 您应该将链接保存在另一个元素中,并在完成循环后单击它以避免此错误