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
- Locate the first link with
By.partialLinkText()and the fragment for the text fields link. - Print its full visible text, read with
getText(), after the labelLink 1:. - Locate the second link with the fragment for the buttons link.
- 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 andfindElement()throwsNoSuchElementException.Using it outside links
Partial link text only searches
aelements. To match text inside a button or a paragraph, use XPath withcontains(), 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.
Related lessons
- Find Element by Link TextThe exact-text version of this locator.
- Find Multiple Elements with findElements()Collect all matches with
findElements()and check how many there are. - XPath Functions - contains() and starts-with()XPath
contains()andstarts-with()for text and attributes on any element.
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.
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();
}
}