Selenium Java-使用增强for循环进行迭代时出现陈旧元素异常

Selenium Java-使用增强for循环进行迭代时出现陈旧元素异常,java,selenium-webdriver,foreach,staleelementreferenceexception,Java,Selenium Webdriver,Foreach,Staleelementreferenceexception,用例: 单击第页上的按钮 它会打开一个新的div弹出窗口(其中包含所有这些父->子-子孩子继承权,以便选择模板-可以有n个子和子孩子) 迭代选择每个子项-但在选择每个子项后还需要执行其他步骤,例如,必须单击“添加到div”弹出窗口,在随后显示的小弹出窗口中输入页面名称,单击“添加”,然后再次导航到前面的页面(使用herirachy)以选择下一个子项 现在,它给出了陈旧的元素引用异常 为了避免过时的元素异常,尝试使用while循环(以及try和catch)尝试至少两次子选择步骤,现在它会执行,但在

用例:

单击第页上的按钮 它会打开一个新的div弹出窗口(其中包含所有这些父->子-子孩子继承权,以便选择模板-可以有n个子和子孩子) 迭代选择每个子项-但在选择每个子项后还需要执行其他步骤,例如,必须单击“添加到div”弹出窗口,在随后显示的小弹出窗口中输入页面名称,单击“添加”,然后再次导航到前面的页面(使用herirachy)以选择下一个子项 现在,它给出了陈旧的元素引用异常

为了避免过时的元素异常,尝试使用while循环(以及try和catch)尝试至少两次子选择步骤,现在它会执行,但在每个循环中它总是选择第一个子-注意,它不会再次进入try-catch循环

参考代码

public static void selectpage(){
driver.findElement(By.xpath(".//[@id='root']/div[2]/
div/div[1]/span[2]")).click();
driver.findElement(By.xpath(".//* 
[@id='root']/div[2]/div[1]/span/span")).click();
            }

public  void pageLayout() throws InterruptedException {

selectpage();

List<WebElement> outerLIElementList = driver.findElements(By.xpath(".//* 
[@id='root']/div/div[1]/div/div[*]"));

System.out.println(outerLIElementList.size());

// iterate through the rows in the outer element
for (WebElement outerLIElement : outerLIElementList) {

// find the inner table rows using the outer table row
List<WebElement> innerLIElementList = 
outerLIElement.findElements(By.xpath("//div[2]/div[*]/section"));
System.out.println(innerLIElementList.size());


// iterate through the inner table rows and sysout
for (WebElement innerLIElement : innerLIElementList) {
Thread.sleep(5000);
int attempts = 0; 

while (attempts<2){
try {

Actions builder = new Actions(driver);
builder.moveToElement(innerLIElement).build().perform();
System.out.println(innerLIElement.getText());

builder.moveToElement(innerLIElement).click(innerLIElement);
builder.perform();  

break;
} 
catch (StaleElementReferenceException e){   
                     } 
                      attempts++;
                    } 
driver.findElement(By.xpath(".//[@id='root']/div/div[2]/button[2]")).click();
driver.findElement(By.xpath(".//[@id='root']/div/div/
/div[2]/div/input")).sendKeys("Test");
driver.findElement(By.xpath(".//*[@id='root']/div/div/
/div[2]/button[2]")).click();
Thread.sleep(1000);
selectpage();                       
publicstaticvoidselectpage(){
driver.findElement(By.xpath(“./[@id='root']]/div[2]/
div/div[1]/span[2]”。单击();
driver.findElement(按.xpath(“.//*
[@id='root']/div[2]/div[1]/span/span”)。单击();
}
public void pageLayout()引发InterruptedException{
选择页面();
List OUTERLIMENTLIST=driver.findElements(By.xpath(“.//*
[@id='root']/div/div[1]/div/div[*]”);
System.out.println(outerElementList.size());
//遍历外部元素中的行
for(WebElement外部元素:外部元素列表){
//使用外部表行查找内部表行
列表内部元素列表=
outerLifement.findElements(By.xpath(“//div[2]/div[*]/section”);
System.out.println(innerLIElementList.size());
//遍历内部表行和sysout
for(WebElement内部元素:内部元素列表){
睡眠(5000);
int=0;

如果我正确地跟随你,那就是你的错误

List<WebElement> outerLIElementList = driver.findElements(By.xpath(".//* 
[@id='root']div/div[*]"));
List outerliementlist=driver.findElements(By.xpath(“.//*
[@id='root']div/div[*]”);
这就过时了。你每次都需要找到元素。我将迭代这个列表的长度,并根据这个迭代找到特定的div

sudo代码:

for(int child = 0; child<len(outerLIElementList); child++)
    driver.findElement(By.xpath(".//*[@id='root']div/div[{0}]", child));

for(int child=0;child感谢您的提示。使用嵌套for循环解决了问题FYI:driver.findElement(By.xpath(“./*[@id='root']div/div[{0}]”,child));由于它给出了语法错误,所以无法工作..我必须传递driver.findElement(By.xpath(“./*[@id='root']div/div['+child+]”)@萨尔很高兴这让你走上正轨;考虑接受答案。