遍历列表时元素未附加到页面文档

遍历列表时元素未附加到页面文档

本文介绍了org.openqa.selenium.StaleElementReferenceException:遍历列表时元素未附加到页面文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么会出现这个错误.需要帮助修复它.我正在开发的网站:"http://freevideolectures.com/Course/3680/Pentaho-BI".在本站

I don't know why this error shows up . Need help to fix it . Website i am working on :"http://freevideolectures.com/Course/3680/Pentaho-BI" . In this site

List<WebElement> cl = d.findElements(By.xpath("//ul[@class='lecture_menu']/li/a"));
System.out.println(cl.size());

for(int e=0 ; e<=cl.size()-1; e++) {
        cl.get(e).click();// i think the error shows up here, the loop runs for e=0 ,
        Thread.sleep(1000);
        String q = d.findElement(By.xpath(".//*[@id='cs-about']/div/div[2]/div[2]/span/a")).getAttribute("href");
        System.out.println(q);
}

循环为 e=0 运行一次,我正在获取该输出.在此之后,错误出现了.错误:过时的元素参考:元素未附加到页面文档.请帮忙 .

The loop runs once for e=0 , i am getting the output for that . After this the error shows up . error: stale element reference: element is not attached to the page document. Help Please .

推荐答案

这是打开 WebBrowser 的示例代码,URLhttp://freevideolectures.com/Course/3680/Pentaho-BI,浏览所有链接 By.xpath("//ul[@class='lecture_menu']/li/a"),在新的 Tab 中打开它们,打印 href 并关闭 Tab :

Here is the sample code which opens the WebBrowser with URL as http://freevideolectures.com/Course/3680/Pentaho-BI, browses through all the links By.xpath("//ul[@class='lecture_menu']/li/a"), opens each of them in a new Tab prints the href and closes the Tab :

    driver.get("http://freevideolectures.com/Course/3680/Pentaho-BI");
    List<WebElement> elementList = driver.findElements(By.xpath("//ul[@class='lecture_menu']//li/a"));
    ArrayList<String> hrefList = new ArrayList<String>();
    for(WebElement element:elementList)
        hrefList.add(element.getAttribute("href"));
    String firstTab =driver.getWindowHandle();
    for(String myhref:hrefList)
    {
        ((JavascriptExecutor) driver).executeScript("window.open(arguments[0])", myhref);
        Set<String> windowHandles = driver.getWindowHandles();
        Iterator<String> itr = windowHandles.iterator();
        while(itr.hasNext())
        {
            String next_tab = itr.next();
            if(!firstTab.equalsIgnoreCase(next_tab))
            {
                driver.switchTo().window(next_tab);
                Thread.sleep(1000);
                String q = driver.findElement(By.xpath(".//*[@id='cs-about']/div/div[2]/div[2]/span/a")).getAttribute("href");
                System.out.println(q);
                driver.close();
                driver.switchTo().window(firstTab);
            }
        }

    }

我控制台上的输出是:

http://keepvid.com/?url=http://www.youtube.com/watch?v=nYI7A9giFzE
http://keepvid.com/?url=http://www.youtube.com/watch?v=YZz8tphl9o4
http://keepvid.com/?url=http://www.youtube.com/watch?v=Z9gSDaQQscE
http://keepvid.com/?url=http://www.youtube.com/watch?v=V0swWI9v-aY
http://keepvid.com/?url=http://www.youtube.com/watch?v=aooocfhp8Pw
http://keepvid.com/?url=http://www.youtube.com/watch?v=pOgWNdmo6Dw
http://keepvid.com/?url=http://www.youtube.com/watch?v=C-M0ESbGmCI
http://keepvid.com/?url=http://www.youtube.com/watch?v=43s93a3aY58
http://keepvid.com/?url=http://www.youtube.com/watch?v=ZzPMJSjQK_U
http://keepvid.com/?url=http://www.youtube.com/watch?v=KWJqbhunb9I
http://keepvid.com/?url=http://www.youtube.com/watch?v=XmcFx0wIKHo
http://keepvid.com/?url=http://www.youtube.com/watch?v=ouU5QwW3YwA
http://keepvid.com/?url=http://www.youtube.com/watch?v=GK-mzNIKyj8
http://keepvid.com/?url=http://www.youtube.com/watch?v=vcrqycyAFGQ
http://keepvid.com/?url=http://www.youtube.com/watch?v=EYLuGIzH9Uo
http://keepvid.com/?url=http://www.youtube.com/watch?v=J8NbYQaQiPo
http://keepvid.com/?url=http://www.youtube.com/watch?v=L6hLwjF45jI
http://keepvid.com/?url=http://www.youtube.com/watch?v=yazzgQ6g2-o
http://keepvid.com/?url=http://www.youtube.com/watch?v=kgO_eJsQVgE
http://keepvid.com/?url=http://www.youtube.com/watch?v=6bYwsbk7e3k
http://keepvid.com/?url=http://www.youtube.com/watch?v=MWRrqjZg4r4
http://keepvid.com/?url=http://www.youtube.com/watch?v=aviCystupUI
http://keepvid.com/?url=http://www.youtube.com/watch?v=CSZ7FDkxGDs
http://keepvid.com/?url=http://www.youtube.com/watch?v=iyEFAQIOrQg
http://keepvid.com/?url=http://www.youtube.com/watch?v=JqfB90OfHTI
http://keepvid.com/?url=http://www.youtube.com/watch?v=CJbb6hNX5wA
http://keepvid.com/?url=http://www.youtube.com/watch?v=8k4u9mdmfi4

这篇关于org.openqa.selenium.StaleElementReferenceException:遍历列表时元素未附加到页面文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:57