Working With Links using Selenium WebDriver

September 24, 2016

Introduction To Links in HTML:

Links in HTML are 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 of the page).

Identifiers in Selenium WebDriver to Interact with Links:

Selenium WebDriver provides two different identifiers to interact with links.

  • By.linktext()
  • By.partialLinktext()

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

partialLinktext() identifier matches partial text associated to 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() etc.

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”.

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

Scenario:

  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.

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. Then size() method will give the count of number of WebElements i.e. number of links.

Main Method:

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

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

Scenario:

  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:

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

About the Author

Saurabh

Follow Saurabh:

Leave a Comment: