0

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

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 运行一次,我得到了那个的输出。在此之后出现错误。错误:过时的元素引用:元素未附加到页面文档。请帮忙 。

4

2 回答 2

0

当您找到要单击的元素时,只需中断循环即可。

原因是即使单击该元素,它仍在查找元素。使用 For -If 循环所以当你得到那个元素时打破它

于 2017-11-17T08:24:56.143 回答
0

以下是WebBrowser使用URLas打开的示例代码http://freevideolectures.com/Course/3680/Pentaho-BI,浏览所有链接 By.xpath("//ul[@class='lecture_menu']/li/a"),在新Tab打印中打开每个链接href并关闭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
于 2017-11-17T16:04:51.110 回答