-1

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();
}
4

1 回答 1

0

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

于 2014-08-26T17:10:21.070 回答