Trying to fetch all the links using java and then want to loop the clicking of each one of them

472 views
Skip to first unread message

Mediha Aziz

unread,
Oct 9, 2013, 12:09:40 PM10/9/13
to seleniu...@googlegroups.com
Hello All Selenium Users, 

I need some expertise to help me out. 

I have been searching and trying different approaches the best one I found is this: 
WebDriver driver= new ChromeDriver();  
Thread.sleep(6000);
driver.get("http://www.google.com"); 

driver.findElement(By.xpath("//*[@id='gbqfq']")).sendKeys("SanDisk 32 GB Memory Card"); 
driver.findElement(By.xpath("//*[@id='gbqfb']/span")).click(); 
List<String> hrefs = new ArrayList<String>();
List<WebElement> links = driver.findElements(By.tagName("a"));
Iterator<WebElement> i = links.iterator();
while(i.hasNext()) {
WebElement link = i.next();
hrefs.add(link.getAttribute("href"));
}

Iterator<String> j = hrefs.iterator();
while(j.hasNext()) {
String href = j.next();
WebElement link = driver.findElement(By.cssSelector("a[href=" + href +"]"));
link.click();
// confirm the link was good
driver.navigate().back();
}

This block of code returns all the links fetched but gives this error : 
"message": "stale element reference: element is not attached to the page document\n  (Session info: chrome=30.0.1599.69)\n  (Driver info: chromedriver=2.0,platform=Windows NT 6.1 SP1 x86_64)"  


I think there should be a better approach like fetching all the links then depositing it into an array and then just loop through that array. 

I am using selenium webdriver 2.35 with java. 

Please help me if possible! 

Regards, 
Mediha  

DongXiang

unread,
Oct 9, 2013, 10:09:53 PM10/9/13
to seleniu...@googlegroups.com
The list stored the links from the first google search page, when you click any link, google navigate to the other page, then you did driver.navigate().back(), google will return to search page, but this time, the search page is not the same as the first one, all links you found in the first one will be stale elements and you will get the exception.
One possible way to re-get those links, delete those links clicked before (you must save those links clicked before). 

Hope it can help you.
David Dong 


Date: Wed, 9 Oct 2013 09:09:40 -0700
From: ny.med...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Trying to fetch all the links using java and then want to loop the clicking of each one of them
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/01a77f02-16cb-4927-a27b-602ca42349ee%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Mediha Aziz

unread,
Oct 10, 2013, 1:41:38 PM10/10/13
to seleniu...@googlegroups.com
Thanks for replying David it didn't even click any link. Also I know we should deposit all the links in an array but I don't seem to get how to do it. 

DongXiang

unread,
Oct 16, 2013, 12:36:17 AM10/16/13
to seleniu...@googlegroups.com
Catch stale excepition and try re-get the element.  this exception tell you the element is on the page but changed after your locate it. re-locate it can solve the problem.

Best Regards,
--david


Date: Thu, 10 Oct 2013 10:41:38 -0700
From: ny.med...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Re: Trying to fetch all the links using java and then want to loop the clicking of each one of them
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages