java - 迭代嵌入链接时出现 StaleElementReferenceException

标签 java selenium staleelementreferenceexception

在我的网页上,我有一个部分链接列表,每个部分都有详细信息的链接。我尝试转到每个部分,然后验证所有链接都没有损坏。

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

问题是在检查第一部分的详细信息后,我收到 StaleElementReferenceException 。我猜这是因为在迭代细节并返回后,Selenium 不会将其余部分列表视为当前部分?

我可能可以创建一个包含所有章节的 href 的列表,然后迭代该列表,导航到特定的章节链接,然后检查详细信息的链接。但也许还有其他/更简单的解决方案?

最佳答案

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

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"));
        }

关于java - 迭代嵌入链接时出现 StaleElementReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50582663/

相关文章:

python - 使用 Python 迭代时出现 StaleElementException

java - 映射、分组、值列表

java - 使用空 List 参数在 Gherkin 中创建 Cucumber 特征

java - 将表达式的结果分配给原语

ruby - 尝试使用 Selenium Webdriver v3.70 最大化浏览器窗口时出现 Ruby "KeyError: key not found: 102"错误

java - Selenium 无法点击 google SERP 中的链接

c# - 用于剑道小部件的 c# 中的 selenium cssselector

java - 使用页面工厂设计模式在页面对象中第二次调用 web 元素会出现陈旧元素异常

java - 无论现有帐户如何,select count(*) 都会返回 true