• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Selenium » Free Selenium Tutorial » Link Text and Partial Link Text In Selenium WebDriver

Link Text and Partial Link Text In Selenium WebDriver

Last Updated on: August 19, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Link Text To Locate an Element
  • Partial Link Text To Locate Element
  • How to Select the Right Link Text When You Have Multiple Match Results

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

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

When we are trying to locate link elements in an application then that time we can locate that element with exact match text or with the partial matching text. So using the Link text or partial link text we can able to 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

Link Text To Locate an Element

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

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 To Locate Element

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 with the use of partial link text we do not look for the exact text match of the string value, you can locate the element with the partial match also. so it will be helpful when a link has the bigger text so that time you can take the help of partial link text to locate the elements.

the syntax for partial link text is looks something like 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();
   }
}

How to Select the Right Link Text When You Have Multiple Match Results

In our previous example, we have discussed and learn 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 that the same link text then it is your responsibility to make sure that clicking on the right links.

So in the below example, we are going to discuss how to find the correct link out of the so 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, link text and partial link text are cases 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 then this is the best locator to go with.

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

    Filed Under: Free Selenium Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers