问题
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);
}
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 .
回答1:
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
回答2:
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
来源:https://stackoverflow.com/questions/47346047/org-openqa-selenium-staleelementreferenceexception-element-is-not-attached-to-t