How to Click a Link by href value in WebDriver

In WebDriver, there are a number of ways we can interact with web elements, such as by element’s ID, Xpath, CSS, etc…

We can also click links by linkText or partialLinkText. These methods are good if we know the expected text between the <a></a> tags. However when we do localization or internationalization testing, the text is translated accordingly and we are no longer able to use linkText or partialLinkText, especially if the anchor tags don’t contain any ID or class.

Suppose we want to click a link which points to profile.html,

e.g.

XHTML

1

<a href="http://www.abc.com/account/profile.html">View Profile</a>

As mentioned earlier, we can use

Java

1

driver.findElement(By.linkText("View Profile")).click()

and that will work, as long as we are on a page with English language. When we change the language to German, our link now displays

XHTML

1

<a href="http://www.abc.com/account/profile.html">Profil ansehen</a>

Now, the line

Java

1

driver.findElement(By.linkText("View Profile")).click()

would fail because there is no link on page with that text.

One way to overcome this is to click a link by its href value, because even when the language of the site changes the href link should still point to the same location. In other words, internationalization shouldn’t impact the href of the links.

WebDriver doesn’t have a standalone and direct way of clicking a link by its href value. Instead, we need to get all the links in the page, extract the href attribute and then compare the href values with what we expect.

Suppose we have the following links and we want to click on the profile link

XHTML

1

<a href="http://abc.com/account/profile.html">View Profile</a>

XHTML

1

<a href="http://abc.com/account/transactions.html">Transactions</a>

Java

1

2

3

4

5

6

7

8

9

10

11

12

publicvoidclickLinkByHref(Stringhref){

List<WebElement>anchors=driver.findElements(By.tagName("a");

Iterator<WebElement>i=anchors.iterator();

while(i.hasNext()){

WebElement anchor=i.next();

if(anchor.getAttribute("href").contains(href)){

anchor.click();

break;

}

}

}

Then we can use clickLinkByHref("Profile")

and then the above function will get all the links on the page and iterate through them and when it finds a link which contains profile, webDriver will click the link.

Note, if we have several links with the word profile in them, the above function will always click the first link containing the word profile. We can modify the above code to include which of the links we want to click: