Link Text and Partial Link Text In Selenium WebDriver

Link Text & Partial Link Text In Selenium: In our previous post about the Java Selenium tutorial, we discussed various topics like Locating elements By ID, Locate elements by Name, Locate elements by Class Name, and Locate elements by Tag Name on a web application. But in this post, we will learn how to locate elements by using the link text and partial link text.

This post will try to understand the real implementation of Link and Partial Link text for automation testing with selenium. In Selenium WebDriver, we have two special types of locators that help us locate the anchor text elements on a web page.

When locating link elements in an application, we can locate that element with exact or partial matching text. So, using the Link or partial link text, we can locate any link elements on a web page.

Other Topics:

  • How To Locate Element By ID Locator
  • How To Locate Element By Name Locator
  • How To Locate Element By Class Name Locator
  • How To Locate Element By Tag Name Locator
  • How To Locate Element By CSS Selector Locator
  • How To Locate Element By XPath Locator

If your targeted element is a Link, you can use the Link text locator to locate an element on the web application page. To locate the element, we can use the text below:

package com.selenium.practice.locator;

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

public class LinkText_Locator
{
   public static void main(String[] args) throws InterruptedException 
   {
      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/08/tag-locator-practice-example-1.html");
      Thread.sleep(10000);

      driver.findElement(By.linkText("Selenium Tutorial")).click();;
      Thread.sleep(5000);
      driver.close();
   }
}

Note: If there are multiple elements that have a sane similar text, then it will automatically select the first one.

Partial Link text is another way to locate the link element of a web page. The only difference between link text and partial link text is that with partial link text, we do not look for the exact text match of the string value; you can also locate the element with the partial match. It will be helpful when a link has bigger text so you can use partial link text to locate the elements.

the syntax for partial link text looks something like the below:

package com.selenium.practice.locator;

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

public class PartialLinkText_Locator
{
   public static void main(String[] args) throws InterruptedException 
   {
      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser driver");

      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/08/tag-locator-practice-example-1.html");
      Thread.sleep(10000);

      driver.findElement(By.partialLinkText("Selenium")).click();;
      Thread.sleep(5000);
      driver.close();
   }
}

In our previous example, we discussed and learned how to locate the web elements using the link text and partial link text. But when You are using link text to find the element, and in return, you are getting multiple elements with the same link text, it is your responsibility to make sure you are clicking on the right links.

So, in the below example, we will discuss how to find the correct link out of the many matching links.

package com.selenium.practice.locator;

import java.util.List;

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

public class Handling_Mulltiple_MatchingLinks
{
   public static void main(String[] args) throws InterruptedException 
   {
      WebDriver driver;
      String RequiredText= "Selenium Programs";
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/08/multiple-element-have-similar-link-text.html");
      Thread.sleep(10000);

      List<WebElement> list=driver.findElements(By.partialLinkText("Selenium"));
      //Printing All The Matching Links
      
      for(int i=0; i< list.size();i++)
      {
         String currentText=list.get(i).getText();
         System.out.println(list.get(i).getText());
         if(RequiredText.equals(currentText))
         {
            list.get(i).click();
            System.out.println("Correct Link Is Clicked");
            //If you are not use below 2 statements then you will get StaleElementReferenceException
            Thread.sleep(5000);
            driver.close();
            //If you are Not Use below statement then you will get NoSuchSessionException
            System.exit(0);
         }
      }
      
   }
}

Note: Both links and partial links are case sensitive as CSS locator in Selenium.

Final Words

Link Text and partial link text of selenium Web Driver work only for handling the link elements of a web application. So, in your application, if you want to deal with the link elements, this is the best locator.

If you still have any queries regarding these two locators, you can comment in the comment section, and we are happy to help clear your queries.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment