Locating Strategies By Partial Link Text Using Java
Last Updated :
26 Jul, 2025
Web application testing using partial link text as a locator strategy is a powerful way to identify clickable links on a web page. Selenium is an open-source framework that allows us to automate web browser testing. Partial Link Text is particularly useful when you only know a portion of the link text. By using partial link text, you can locate a link even if you don’t have the exact full text.
Here are the steps for using the By Partial Link Text in Selenium with Java:
Steps for Locating Elements Using Partial Link Text in Java
Here are the steps of locating strategies By.partialLinkText:
Step 1: Set up Your Project
To get started with Selenium WebDriver, make sure you have the following setup:
- Java Development Kit (JDK) installed on your system to compile and run Java applications.
- Selenium WebDriver added to your project dependencies, which allows you to interact with web elements and automate browsers.
- Maven or Gradle for managing your project's dependencies, including Selenium WebDriver, to ensure easy and efficient integration.
If using Maven, your pom.xml
should include the following dependency:
pom.xml
XML
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.13.0</version>
</dependency>
</dependencies>
Step 2: Building the class.
Now we will develop the main class. We will create a simple automation in this guide. The task we will automate contains steps:
- Open the Browser
- Locate the link we want to visit using the link text
- Navigate to the link
- Close the Browser
Without any human interactions. Modify the code in your main class as follows:
PartialLinkdemo.java
Main,java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class PartialLinkdemo {
public static void main(String[] args) {
// Set the path to the ChromeDriver (make sure to set the correct path on your system)
System.setProperty("webdriver.chrome.driver", "C:\\Users\\change the path of the chromedriver\\drivers\\chromedriver.exe");
// Initialize the WebDriver
WebDriver driver = new ChromeDriver();
// Open the desired webpage
driver.get("https://www.geeksforgeeks.org/");
// Locate the element using partial link text
WebElement partialLinkElement = driver.findElement(By.partialLinkText("DSA"));
// Click on the link
partialLinkElement.click();
// Print confirmation
System.out.println("Partial Linktext Locator check was successful");
// Close the browser
driver.quit();
}
}
Step 3: Testing the Automation.
Now, we will test the automation we have just created. Upon running the code, we get the output of this is as:
Output of PartialLinkTestWe learned Web application testing will be easily automated using Selenium, which is an open-source framework. Locate elements using partial link text using Java and navigate web pages without the help of humans. Using the outlined steps and adding necessary dependencies easily across various browsers.