Get all the links in a webpage and click one by one

201 views
Skip to first unread message

SriHari Innamuri

unread,
Jun 14, 2011, 6:31:36 AM6/14/11
to Selenium Users
how to get all the links into an array and read one by one, if my
input input link name matches then it has to click on that. How to do
this process in selenium with java. Any one of you please give the
code.

Sarvesh Singh

unread,
Jun 20, 2011, 1:47:53 AM6/20/11
to seleniu...@googlegroups.com

document.links.length will return an integer count of links in the page.

- sarvesh

Mark Collin

unread,
Jun 21, 2011, 9:42:45 AM6/21/11
to seleniu...@googlegroups.com
String linkName = "My Link Name";
List<WebElement> elementList = driver.findElements(By.xpath("//a"));
for(WebElement myElement : elementList){
if(myElement.getText().equals(linkName)){
myElement.click();
}
}

--
You received this message because you are subscribed to the Google Groups
"Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to
selenium-user...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/selenium-users?hl=en.


--
This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

If you have received this email in error please notify postm...@ardescosolutions.com

Luke Inman-Semerau

unread,
Jun 21, 2011, 10:49:32 AM6/21/11
to seleniu...@googlegroups.com
That will break if there's more than one link that matches linkName.

If you want web crawler like functionality (think the google search
bot). Then you need to maintain a list of pages to visit (full urls)
and pages you have visited. Then for every page you visit you get the
link destination without clicking it (href attribute is a common way
to get this), check that you haven't visited it yet and then add it to
you to visit list.

-Luke

On Jun 21, 2011, at 6:42 AM, "Mark Collin" <ma...@ardescosolutions.com>
wrote:

Mark Collin

unread,
Jun 21, 2011, 11:09:16 AM6/21/11
to seleniu...@googlegroups.com
In that case:

String linkName = "My Link Name";

List<WebElement> elementList = driver.findElements(By.xpath("//a"));


for(WebElement myElement : elementList){
if(myElement.getText().equals(linkName)){
myElement.click();

break;
}
}

It's just an example to be expanded upon, but it should give an idea of how
to do it.

You could also iterate a counter to find out the number of links, or click
on the link and then go back and try then next link e.g.:

if(myElement.getText().equals(linkName)){
myElement.click();
driver.navigate().back();
}

The possibilities are endless... ;)

-Luke


--
This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

Reply all
Reply to author
Forward
0 new messages