Skip to content
CodeKruPro

Find Element by Partial Link Text

9/70

Video walkthrough

Watch on YouTube

Find Element by Partial Link Text

The By.partialLinkText() locator finds a link using part of its visible text. This is useful when link text is long or dynamic.

On the Links page, there are three "Practice" links:

  • Practice TextFields
  • Practice Buttons
  • Practice Checkboxes

Task:

  1. Navigate to https://testkru.com/Elements/Links
  2. Find the link containing text TextFields
  3. Print its full text
  4. Find the link containing text Buttons
  5. Print its full text

Expected output:

Link 1: Practice TextFields
Link 2: Practice Buttons

About this lesson

By.partialLinkText in Selenium Java: match part of a link's text

Learn Selenium JavaBeginnerLocators

How it works

By.partialLinkText("text") finds a link whose visible text contains the string anywhere. It helps when a label is long or carries a changing part, such as a count or a date, and only a stable fragment is worth matching.

The looser match has a cost: several links can qualify. The Links page has three Practice links, so By.partialLinkText("Practice") matches all three and findElement() returns the first in the page. Choose a fragment that only one link contains, such as TextFields.

Like By.linkText(), it is case-sensitive and ignores links that are not displayed. In the default headless window the sidebar, with its Radio Buttons and Buttons links, sits off-screen, so the fragment Buttons matches only one link in the page body. In a full-size desktop window the same fragment returns Radio Buttons first, a reminder that a fragment must be unique on the page as rendered.

About the assignment

You will find two of the Practice links by a distinctive fragment of each label, TextFields and Buttons, and print the full text of the link each fragment matched.

What you will do

  1. Locate the first link with By.partialLinkText() and the fragment for the text fields link.
  2. Print its full visible text, read with getText(), after the label Link 1:.
  3. Locate the second link with the fragment for the buttons link.
  4. Print its full text after the label Link 2:.

Common mistakes

  • A fragment that matches several links

    No error tells you. findElement() silently returns the first match, so a test can click the wrong link and fail much later on an unexpected page.

  • Lower-case fragments

    By.partialLinkText("practice") matches nothing on this page and findElement() throws NoSuchElementException.

  • Using it outside links

    Partial link text only searches a elements. To match text inside a button or a paragraph, use XPath with contains(), covered in lesson 14.

In a real test suite

Partial matches are common for links such as Order 1042 details or Inbox (3), where the number changes between runs. Pair the locator with findElements() and an assertion on the size when you need to be sure it is unique.

Lesson 10 takes that idea further and collects every matching element into a list.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/Links");
        
        // TODO: Find link containing "TextFields" and print its full text
        // TODO: Find link containing "Buttons" and print its full text
        
        driver.quit();
    }
}