迭代嵌入式链接时出现StaleElementReferenceException

时间:2018-05-29 10:57:18

标签: 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的列表,然后遍历该列表导航到特定部分链接,然后检查详细信息的链接。但也许还有一些其他/更简单的解决方案?

1 个答案:

答案 0 :(得分:2)

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

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