org.openqa.selenium.StaleElementReferenceException: element is not attached to the page document while iterating through a List

后端 未结 2 1245
生来不讨喜
生来不讨喜 2021-01-27 23:58

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

Li         


        
相关标签:
2条回答
  • 2021-01-28 00:15

    Just break the loop when you find the element you want to click on it.

    Reason is it is finding element even though that element is clicked. Use For -If Loop so when you get that element break it

    0 讨论(0)
  • 2021-01-28 00:27

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

    The Output on my Console is :

    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
    
    0 讨论(0)
提交回复
热议问题