Java 迭代嵌入链接时发生StaleElementReferenceException

Java 迭代嵌入链接时发生StaleElementReferenceException,java,selenium,staleelementreferenceexception,Java,Selenium,Staleelementreferenceexception,在我的网页上,我有一个指向各个部分的链接列表,每个部分都有指向详细信息的链接。我试着去到每个部分,然后验证所有的链接都没有断开 List<WebElement> sections = driver.findElements(By.xpath("//*[@id='sections']/li/a")); System.out.println("sections: " + sections.size()); sections.forEach(selement

在我的网页上,我有一个指向各个部分的链接列表,每个部分都有指向详细信息的链接。我试着去到每个部分,然后验证所有的链接都没有断开

List<WebElement> sections = driver.findElements(By.xpath("//*[@id='sections']/li/a"));
        System.out.println("sections: " + sections.size());
        sections.forEach(selement -> { 
            selement.click();
            List<WebElement> details = driver.findElements(By.xpath("//*[@id='details']/div/table/tbody/tr/td/table[1]/tbody/tr/td[2]/strong/a"));
            System.out.println("details: " + details.size());
            details.forEach(delement -> {
                url = delement.getAttribute("href");
                try {
                    huc = (HttpURLConnection) new URL(url).openConnection();
                    huc.setRequestMethod("HEAD");
                    huc.connect();
                    respCode = huc.getResponseCode();
                    if(respCode == 404) {
                        System.out.println(url + " link is broken");
                    } else if (respCode == 200) {
                        System.out.println(url + " link is ok");
                    } else {
                        System.out.println(url + " returned code " + respCode);
                    }
                    huc.disconnect();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            driver.navigate().back();
        });
List sections=driver.findElements(By.xpath(“/*[@id='sections']]/li/a”);
System.out.println(“sections:+sections.size());
节.forEach(选择->{
selement.click();
List details=driver.findElements(By.xpath(“/*[@id='details']/div/table/tbody/tr/td/table[1]/tbody/tr/td[2]/strong/a”);
System.out.println(“细节:+details.size());
详细信息。forEach(删除->{
url=delement.getAttribute(“href”);
试一试{
huc=(HttpURLConnection)新URL(URL).openConnection();
huc.setRequestMethod(“HEAD”);
huc.connect();
respCode=huc.getResponseCode();
如果(respCode==404){
System.out.println(url+“链接已断开”);
}否则如果(respCode==200){
System.out.println(url+“链接正常”);
}否则{
System.out.println(url+“返回代码”+响应代码);
}
huc.disconnect();
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
});
driver.navigate().back();
});
问题是在检查了第一部分的详细信息之后,我得到了一个
StaleElementReferenceException
。我猜这是因为在反复讨论细节并返回之后,Selenium没有将其余部分列表视为当前内容


我可能会为章节创建一个所有HREF的列表,然后遍历该列表,导航到特定的章节链接,然后检查详细信息的链接。但也许还有其他更简单的解决方案?

是的,返回主页后,您是对的。列表元素正在更改,即使相同,也不会引用相同的元素。 不能在第一次/外部迭代中使用for each。您可以按如下方式更改它。也。返回后,应重新识别/搜索列表元素

List<WebElement> sections = driver.findElements(By.xpath("//*[@id='sections']/li/a"));
        System.out.println("sections: " + sections.size());
        for(int i=0;i<sections.size();i++){ 
            WebElement selement = sections.get(i);
            selement.click();
            List<WebElement> details = driver.findElements(By.xpath("//*[@id='details']/div/table/tbody/tr/td/table[1]/tbody/tr/td[2]/strong/a"));
            System.out.println("details: " + details.size());
            details.forEach(delement -> {
                url = delement.getAttribute("href");
                try {
                    huc = (HttpURLConnection) new URL(url).openConnection();
                    huc.setRequestMethod("HEAD");
                    huc.connect();
                    respCode = huc.getResponseCode();
                    if(respCode == 404) {
                        System.out.println(url + " link is broken");
                    } else if (respCode == 200) {
                        System.out.println(url + " link is ok");
                    } else {
                        System.out.println(url + " returned code " + respCode);
                    }
                    huc.disconnect();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            driver.navigate().back();
            sections = driver.findElements(By.xpath("//*[@id='sections']/li/a"));
        }
List sections=driver.findElements(By.xpath(“/*[@id='sections']]/li/a”);
System.out.println(“sections:+sections.size());
对于(int i=0;i{
url=delement.getAttribute(“href”);
试一试{
huc=(HttpURLConnection)新URL(URL).openConnection();
huc.setRequestMethod(“HEAD”);
huc.connect();
respCode=huc.getResponseCode();
如果(respCode==404){
System.out.println(url+“链接已断开”);
}否则如果(respCode==200){
System.out.println(url+“链接正常”);
}否则{
System.out.println(url+“返回代码”+响应代码);
}
huc.disconnect();
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
});
driver.navigate().back();
sections=driver.findElements(By.xpath(“//*[@id='sections']]/li/a”);
}

是的,返回主页后,您是正确的。列表元素正在更改,即使相同,也不会引用相同的元素。 不能在第一次/外部迭代中使用for each。您可以按如下方式更改它。也。返回后,应重新识别/搜索列表元素

List<WebElement> sections = driver.findElements(By.xpath("//*[@id='sections']/li/a"));
        System.out.println("sections: " + sections.size());
        for(int i=0;i<sections.size();i++){ 
            WebElement selement = sections.get(i);
            selement.click();
            List<WebElement> details = driver.findElements(By.xpath("//*[@id='details']/div/table/tbody/tr/td/table[1]/tbody/tr/td[2]/strong/a"));
            System.out.println("details: " + details.size());
            details.forEach(delement -> {
                url = delement.getAttribute("href");
                try {
                    huc = (HttpURLConnection) new URL(url).openConnection();
                    huc.setRequestMethod("HEAD");
                    huc.connect();
                    respCode = huc.getResponseCode();
                    if(respCode == 404) {
                        System.out.println(url + " link is broken");
                    } else if (respCode == 200) {
                        System.out.println(url + " link is ok");
                    } else {
                        System.out.println(url + " returned code " + respCode);
                    }
                    huc.disconnect();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            driver.navigate().back();
            sections = driver.findElements(By.xpath("//*[@id='sections']/li/a"));
        }
List sections=driver.findElements(By.xpath(“/*[@id='sections']]/li/a”);
System.out.println(“sections:+sections.size());
对于(int i=0;i{
url=delement.getAttribute(“href”);
试一试{
huc=(HttpURLConnection)新URL(URL).openConnection();
huc.setRequestMethod(“HEAD”);
huc.connect();
respCode=huc.getResponseCode();
如果(respCode==404){
System.out.println(url+“链接已断开”);
}否则如果(respCode==200){
System.out.println(url+“链接正常”);
}否则{
System.out.println(url+“返回代码”+响应代码);
}
huc.disconnect();
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
});
driver.navigate().back();
sections=driver.findElements(By.xpath(“//*[@id='sections']]/li/a”);
}