Working With Links using Selenium WebDriver

a couple of years ago

Links in HTML are nothing but hyperlinks. When we click on a link, we are navigated to another page or document.

HTML links are defined with anchor (<a>) tags.

Syntax: <a href=”url”> link text </a>

Example:

Here in above code, href is an attribute which specifies the address of another HTML page and “Selenium Tutorial” is the link text associated with it. (i.e. the visible part on the page).

Identifiers in Selenium WebDriver to Interact with Links:

Selenium WebDriver provides two different identifiers to interact with links.

  • linktext()
  • partialLinktext()

linktext() identifier matches exact text associated with the link web element. So, to click on above-mentioned link code will be:

partialLinktext() identifier matches partial text associated with the link web element. It can be considered as a contains method. So, to click on above-mentioned link code will be:

Actions performed on Links:

We can perform different actions on a link like click(), getText(), moverHover().

The click() method is to click on the mentioned link, this will navigate you to another webpage in the same window or in another window.

The getText() method is to fetch the text from a link. Below code will return “Selenium Tutorial”.

The mouseHover() operation we will learn in detail in later classes.

Getting Attribute from an HTML Tag:

One of the important scenario while working with links is to get the URL from the link. getAttribute(“String”) method is used to get the url from the link. See the below example, for above HTML link the code will return “http://qatechub.com/selenium”.

Let us try some interview based scenarios:

Scenario 1: Counting numbers of link on a web page say Flipkart

  1. Open any browser say “Chrome”
  2. Navigate to Flipkart website. (http://www.flipkart.com)
  3. Write a code to get the number of links on this page.

To get the number of links on a page, we will first get all the links using a method called findElements(By.tagName(“a”)). This method will return a list of all the WebElements in a list. Call size() method to get the count of the number of WebElements i.e. number of links.

To Execute the code let us create a main method:

In above code, getNumberOfLinks() method is used to get the number of links from a page.

Scenario 2: Printing the text associated with all the links and their URL on a page say Flipkart.

  1. Open any browser say “Chrome.”
  2. Navigate to Flipkart site (http://www.flipkart.com)
  3. Write a code to get the number of links on this page.
  4. Write a code to get the text associated with each link and its URL.

This scenario is an extension of the above scenario. Here, after finding the number of links. We will iterate over each link and fetch its text using a method called getText() and fetch its URL by a method called getAttribute(“href”).

Refer below code for the same:

Enhanced for loop is used to iterate over each link, fetching its text and associated url.

This is all in this tutorial, I hope you enjoyed the different scenario’s possible with links using Selenium WebDriver.

For any questions, queries or comments feel free to write us at support@qatechhub.com or saurabh@qatechub.com. Happy Learning 🙂

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra:

Leave a Comment: