What is LinkText in Selenium?

A link text in Selenium is used to identify the hyperlinks on web page. It is determined or identified with help of an Anchor tag.

Anchor tag is denoted by <a> tag whenever we inspect any link text on web page which clearly indicates that it is hyperlink and can be used as link text to be identified in selenium webdriver.

For creating the hyperlinks on a web page, we can use an anchor tag followed by the link Text.

Links matching a criterion

Links can be accessed using an exact or partial match of their link text, it basically means link text can be identify using exact link text present or using some part of link text or partial link text.

In this tutorial, we will learn the available methods to find and access the Links using Webdriver. Also, we will discuss some of the common problems faced while accessing Links and will further discuss on how to resolve them.

Accessing links using Exact Text Match: By.linkText()

In this link text is identified using the exact text match that we have inspected and using By.linkText() the element can be worked with on web page. However, if there are two links that have the very same link text, this method will only access the first one.

Let us understand this concept using below example.

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LinkText {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        //Code to maximize chrome browser
        driver.manage().window().maximize();

        driver.get("https://rahulshettyacademy.com/AutomationPractice/");

        //Code to match link text with exact match.
        driver.findElement(By.linkText("REST API")).click();

        //Code to close chrome driver
        driver.close();

    }
}

Now let us consider scenario where we have multiple link text with same text, webdriver will identify first and act on that element.

Below is the screenshot that indicates we have multiple link text with same text.

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LinkText {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//Code to maximize chrome browser
driver.manage().window().maximize();

driver.get("https://rahulshettyacademy.com/AutomationPractice/");

//Code to match link text with exact match.
driver.findElement(By.linkText("Dummy Content for Testing.")).click();

//Code to close chrome driver
driver.close();

}
}

The above code will search for first instance of “Dummy Content for Testing.” from left to right since driver start searching from left and moves to right side, the moment webdriver finds first instance of “Dummy Content for Testing.” It will act on that link.

Accessing links using Partial Text Match: By.partialLinkText()

Accessing links using a portion of their link text is done using the By.partialLinkText() method. If you specify a partial link text that has multiple matches, only the first match will be accessed.

Let us understand this with example and code.

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LinkText {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//Code to maximize chrome browser
driver.manage().window().maximize();

driver.get("https://rahulshettyacademy.com/AutomationPractice/");

//Code to match link text with partial text.
driver.findElement(By.partialLinkText("Soa")).click();

//Code to close chrome driver
driver.close();

}
}

How to get Multiple links with the same Link Text

In this section we will see how to solve problem of multiple links with same link text.

In cases where there are multiple links with the same link text, and we want to access the links other than the first one, how do we go about it?

In such cases, generally, different locators viz... By.xpath(), By.cssSelector() or By.tagName() are used.

Most commonly used is By.xpath(). It is the most reliable one, but it looks complex and non-readable too.

Case-sensitivity for Link Text

The parameters for By.linkText() and By.partialLinkText() are both case-sensitive, meaning that capitalization matters.

For example, in https://rahulshettyacademy.com/AutomationPractice/# homepage, there are two links that contain the text "ap" - one is the "REST API” link, and the other is the "SoapUI" link.

Though both links contain the character sequence "ap," "By.partialLinkText()" method will access these two links separately depending on the capitalization of the characters. See the sample code below.

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LinkText {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        //Code to maximize chrome browser
        driver.manage().window().maximize();

        driver.get("https://rahulshettyacademy.com/AutomationPractice/");

        //Code to match link text with partial text and navigate to Rest API page
        driver.findElement(By.partialLinkText("AP")).click();
        driver.navigate().back();
        //Code to match link text with partial text and navigate to SoapUI page.
        driver.findElement(By.partialLinkText("ap")).click();
        driver.navigate().back();

        //Code to close chrome driver
        driver.close();

    }
}

Summary

  • Links are accessed using the click() method.
  • Apart from the locators available for any WebElement, Links also have link text based locators:
  • By.linkText() – locates the links based on the exact match of the link's text provided as a parameter.
  • By.partialLinkText() – locates links based on the partial text match of the link's text.
  • Both the above locators are case Sensitive.
  • If there are multiple matches, By.linkText() and By.partialLinkText() will only select the first match. In such cases where multiple links with the same link text are present, other locators based on xpath, CSS are used.
  • findElements() & By.tagName("a") method finds all the elements in the page matching the locator criteria.

Tags


You may also like

API Basics and Architecture

API Basics and Architecture
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}