Partial Link Text in Selenium
Partial Link Text is a Selenium locator strategy used to identify a web link by matching only a part of the visible text of an anchor element. It is especially useful when the complete link text is long, dynamic, or when only a unique portion of the link text is known.
In Selenium WebDriver, Partial Link Text is implemented using the By.partialLinkText() method in Java. It works with HTML <a> elements and searches for links whose visible text contains the specified partial text.
JustAcademy's Selenium training curriculum includes different locator strategies for identifying web elements, including Link Text and Partial Link Text. You can learn more through the JustAcademy's Selenium Training Course and explore the Register for Course Demo option.
1. What is Partial Link Text?
Partial Link Text is a Selenium locator used to locate an anchor link when you know only a portion of its visible text.
For example, suppose a webpage contains the following link:
<a href="https://example.com/courses">View All Selenium Courses</a>
If the complete text is View All Selenium Courses, Selenium can locate the link using only a unique portion such as Selenium Courses.
driver.findElement(By.partialLinkText("Selenium Courses")).click();
Selenium searches the page for an anchor element whose visible text contains the specified partial text.
2. Why Use Partial Link Text?
Partial Link Text is useful when the complete link text is lengthy or when only a stable portion of the text is available.
- It can locate links using only part of their visible text.
- It is useful for long link names.
- It can be helpful when some portions of link text are dynamic.
- It provides a simple alternative to XPath for certain links.
- It is easy to understand for beginners.
- It can make some test scripts shorter.
3. Basic Syntax
The basic Java Selenium syntax is:
driver.findElement(By.partialLinkText("partial text"));
To click the identified link:
driver.findElement(By.partialLinkText("partial text")).click();
4. Import Required Classes
To use Partial Link Text with Selenium Java, import the Selenium classes required for WebDriver and locators.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
5. Complete Basic Example
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class PartialLinkTextExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
driver.findElement(By.partialLinkText("Courses")).click();
driver.quit();
}
}
In this example, Selenium searches for a link whose visible text contains Courses.
6. HTML Example
<a href="/selenium">Learn Selenium Automation Testing</a>
The complete visible text is Learn Selenium Automation Testing.
A partial text such as Selenium can be used:
driver.findElement(By.partialLinkText("Selenium")).click();
7. Partial Link Text vs Link Text
Link Text generally requires the complete visible text of the link, whereas Partial Link Text uses a portion of the visible text.
| Feature | Link Text | Partial Link Text |
| Method | By.linkText() | By.partialLinkText() |
| Text required | Complete link text | Part of link text |
| Element type | Anchor link | Anchor link |
| Example | By.linkText("Login to Account") | By.partialLinkText("Login") |
| Useful for long text | Less convenient | More convenient |
8. Example of Link Text
<a href="/login">Login to Your Account</a>
Using complete Link Text:
driver.findElement(By.linkText("Login to Your Account")).click();
9. Example of Partial Link Text
The same link can be located using a portion of its text:
driver.findElement(By.partialLinkText("Login")).click();
Another possible partial value is:
driver.findElement(By.partialLinkText("Account")).click();
10. How Partial Link Text Works
The general process is:
Web Page
↓
HTML Anchor Elements
↓
Visible Link Text
↓
Partial Text Provided
↓
Selenium Searches Matching Links
↓
Matching WebElement
↓
Action such as click()
11. Partial Link Text Works with Anchor Elements
Partial Link Text is designed for links represented by the HTML <a> element.
<a href="/about">About Our Company</a>
<a href="/contact">Contact Us</a>
For the first link:
driver.findElement(By.partialLinkText("About")).click();
For the second link:
driver.findElement(By.partialLinkText("Contact")).click();
12. Locating a Link with a Unique Partial Text
A partial value should preferably identify one specific link.
<a href="/training">Selenium Automation Training</a>
<a href="/java">Core Java Training</a>
A good locator is:
driver.findElement(By.partialLinkText("Selenium Automation")).click();
13. Importance of Unique Partial Text
If several links contain the same partial text, Selenium may identify the wrong element because the locator is not sufficiently specific.
<a href="/selenium">Learn Selenium</a>
<a href="/selenium-course">Selenium Course</a>
<a href="/selenium-project">Selenium Project</a>
Using:
driver.findElement(By.partialLinkText("Selenium")).click();
may match more than one link.
14. Avoid Overly Generic Partial Text
Generic words such as Click, More, Learn, View, or Read can occur in many links.
<a href="/java">Learn Java</a>
<a href="/selenium">Learn Selenium</a>
<a href="/python">Learn Python</a>
This is less specific:
driver.findElement(By.partialLinkText("Learn")).click();
A more specific locator is:
driver.findElement(By.partialLinkText("Selenium")).click();
15. Partial Link Text with click()
The most common action performed after locating a link is click().
driver.findElement(By.partialLinkText("Login")).click();
The statement finds a matching link and clicks it.
16. Partial Link Text with getText()
You can retrieve the complete visible text of the identified link using getText().
String linkText = driver.findElement(By.partialLinkText("Selenium")).getText();
System.out.println(linkText);
If the actual link text is Learn Selenium Automation, the output can be:
Learn Selenium Automation
17. Partial Link Text with isDisplayed()
You can verify whether the matching link is displayed.
boolean displayed = driver.findElement(By.partialLinkText("Login")).isDisplayed();
System.out.println(displayed);
18. Partial Link Text with isEnabled()
You can check whether the located web element is enabled.
boolean enabled = driver.findElement(By.partialLinkText("Continue")).isEnabled();
System.out.println(enabled);
19. Partial Link Text with isSelected()
isSelected() is generally used for checkboxes, radio buttons, and selectable elements rather than ordinary links.
boolean selected = driver.findElement(By.partialLinkText("Option")).isSelected();
For normal hyperlinks, isSelected() is generally not useful.
20. Using Partial Link Text in a Login Scenario
<a href="/login">Login to Your Account</a>
Selenium code:
driver.findElement(By.partialLinkText("Login")).click();
21. Using Partial Link Text in a Registration Scenario
<a href="/register">Create a New Account</a>
Automation:
driver.findElement(By.partialLinkText("Create")).click();
22. Using Partial Link Text for Forgot Password
<a href="/forgot-password">Forgot Your Password?</a>
Automation:
driver.findElement(By.partialLinkText("Forgot")).click();
23. Using Partial Link Text for Course Links
<a href="/selenium-course">Selenium Automation Testing Course</a>
A partial link locator can be:
driver.findElement(By.partialLinkText("Selenium Automation")).click();
24. Partial Link Text with Multiple Links
<a href="/selenium">Selenium Training</a>
<a href="/java">Java Training</a>
<a href="/python">Python Training</a>
To select the Selenium link:
driver.findElement(By.partialLinkText("Selenium")).click();
25. Partial Link Text with findElements()
findElements() can be used to retrieve multiple matching elements.
List<WebElement> links = driver.findElements(By.partialLinkText("Training"));
Required import:
import java.util.List;
import org.openqa.selenium.WebElement;
26. Count Links Matching Partial Text
List<WebElement> links = driver.findElements(By.partialLinkText("Training"));
System.out.println("Matching links: " + links.size());
This is useful when validating navigation menus or lists of course links.
27. Loop Through Matching Links
List<WebElement> links = driver.findElements(By.partialLinkText("Training"));
for (WebElement link : links) {
System.out.println(link.getText());
}
28. Verify a Link Exists
List<WebElement> links = driver.findElements(By.partialLinkText("Selenium"));
if (!links.isEmpty()) {
System.out.println("Selenium link is present");
} else {
System.out.println("Selenium link is not present");
}
29. Partial Link Text with Explicit Wait
Modern websites may load links dynamically. An explicit wait can be used before interacting with the link.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement link = wait.until(
ExpectedConditions.elementToBeClickable(
By.partialLinkText("Login")
)
);
link.click();
30. Required Imports for Explicit Wait
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
31. Partial Link Text with Dynamic Content
Suppose a website generates a link whose text contains a changing value.
<a href="/order/12345">View Order 12345</a>
If the stable portion is View Order, a partial link locator may be used:
driver.findElement(By.partialLinkText("View Order")).click();
However, the locator should be used only when the matching text is sufficiently unique.
32. Partial Link Text for Dynamic User Names
<a href="/profile">View Profile - Rahul</a>
Instead of depending on the changing username, a stable partial value can be used:
driver.findElement(By.partialLinkText("View Profile")).click();
33. Partial Link Text for Dynamic Course Names
<a href="/course">Enroll in Selenium Automation Course</a>
A partial locator can target a stable phrase:
driver.findElement(By.partialLinkText("Enroll in Selenium")).click();
34. Partial Link Text and Case Sensitivity
Partial Link Text should be written carefully according to the visible link text. Avoid assuming that different capitalization will always produce the desired match.
driver.findElement(By.partialLinkText("Selenium")).click();
When locator behavior becomes uncertain, inspect the actual rendered text in browser developer tools.
35. Whitespace in Link Text
Visible link text can contain spaces or formatting that makes exact matching difficult. Partial Link Text can sometimes simplify the locator by using a stable portion.
<a href="/course"> Selenium Automation Course </a>
Example:
driver.findElement(By.partialLinkText("Selenium Automation")).click();
36. Partial Link Text with Navigation Menus
<nav>
<a href="/home">Home</a>
<a href="/courses">All Selenium Courses</a>
<a href="/contact">Contact Us</a>
</nav>
To click the course navigation link:
driver.findElement(By.partialLinkText("Selenium Courses")).click();
37. Partial Link Text in E-Commerce Applications
<a href="/product/100">View Product Details</a>
Automation:
driver.findElement(By.partialLinkText("Product Details")).click();
38. Partial Link Text in News Websites
<a href="/article/1">Read Latest Technology News</a>
Automation:
driver.findElement(By.partialLinkText("Technology News")).click();
39. Partial Link Text in Banking Applications
<a href="/transactions">View Recent Transactions</a>
Automation:
driver.findElement(By.partialLinkText("Recent Transactions")).click();
40. Partial Link Text in Admin Panels
<a href="/users">Manage Registered Users</a>
Automation:
driver.findElement(By.partialLinkText("Registered Users")).click();
41. Partial Link Text for Footer Links
<a href="/privacy">Privacy Policy</a>
<a href="/terms">Terms and Conditions</a>
<a href="/support">Customer Support Center</a>
Examples:
driver.findElement(By.partialLinkText("Privacy")).click();
driver.findElement(By.partialLinkText("Support")).click();
42. Partial Link Text for Help Links
<a href="/help">Visit Help Center</a>
driver.findElement(By.partialLinkText("Help Center")).click();
43. Partial Link Text for External Links
<a href="https://example.com">Visit Example Website</a>
Selenium can locate the link using part of its visible text:
driver.findElement(By.partialLinkText("Example Website")).click();
44. Partial Link Text and Target Attribute
A link may open another tab or window using target="_blank".
<a href="https://example.com" target="_blank">Open Example Website</a>
Partial Link Text can still locate the link:
driver.findElement(By.partialLinkText("Example")).click();
If a new window opens, window handling must be performed separately.
45. Partial Link Text and Multiple Matching Elements
Consider:
<a href="/course1">Selenium Course</a>
<a href="/course2">Selenium Advanced Course</a>
Using:
driver.findElement(By.partialLinkText("Selenium")).click();
may match the first suitable link rather than the link you intended.
In such cases, use a more specific locator.
46. When Partial Link Text Should Be Avoided
- When several links contain the same partial text.
- When the visible text changes frequently.
- When the partial text is extremely generic.
- When a stable ID is available and unique.
- When CSS Selector or XPath provides a clearer and more stable locator.
- When the element is not an anchor link.
47. Partial Link Text vs ID
| Point | Partial Link Text | ID |
| Target | Usually links | Any element with an ID |
| Based on | Visible link text | HTML id attribute |
| Syntax | By.partialLinkText() | By.id() |
| Stability | Depends on link text | Usually strong when ID is stable and unique |
48. Partial Link Text vs Name
The Name locator identifies elements using the HTML name attribute, while Partial Link Text uses visible link text.
driver.findElement(By.name("username"));
driver.findElement(By.partialLinkText("Login"));
49. Partial Link Text vs Class Name
Class Name uses the HTML class attribute, whereas Partial Link Text uses the visible text of an anchor.
driver.findElement(By.className("menu-link"));
driver.findElement(By.partialLinkText("Courses"));
50. Partial Link Text vs CSS Selector
CSS Selector can identify links using attributes, classes, IDs, and other CSS patterns.
driver.findElement(By.cssSelector("a[href='/courses']"));
Partial Link Text focuses specifically on visible link text.
51. Partial Link Text vs XPath
XPath provides more advanced relationships and text-based expressions.
driver.findElement(By.xpath("//a[contains(text(),'Selenium')]")).click();
This can be useful when Partial Link Text is not specific enough or when the element needs to be identified using additional conditions.
52. Equivalent XPath for Partial Link Text
For an anchor containing specific text, XPath can provide similar behavior:
driver.findElement(By.xpath("//a[contains(.,'Selenium')]")).click();
The XPath approach can also be combined with attributes or other conditions.
53. Partial Link Text and XPath Comparison
| Feature | Partial Link Text | XPath |
| Syntax | Simple | More expressive |
| Text matching | Partial visible link text | Can use text functions and relationships |
| Complex conditions | Limited | Supported |
| Anchor-focused | Yes | No |
| Learning difficulty | Easy | Moderate |
54. Partial Link Text for Login Link
<a href="/login">Login to Your Account</a>
driver.findElement(By.partialLinkText("Login")).click();
55. Partial Link Text for Sign Up Link
<a href="/register">Create Your Free Account</a>
driver.findElement(By.partialLinkText("Free Account")).click();
56. Partial Link Text for Logout
<a href="/logout">Logout from Application</a>
driver.findElement(By.partialLinkText("Logout")).click();
57. Partial Link Text for Dashboard
<a href="/dashboard">Go to User Dashboard</a>
driver.findElement(By.partialLinkText("User Dashboard")).click();
58. Partial Link Text for Profile
<a href="/profile">View My Profile</a>
driver.findElement(By.partialLinkText("My Profile")).click();
59. Partial Link Text for Orders
<a href="/orders">View All Previous Orders</a>
driver.findElement(By.partialLinkText("Previous Orders")).click();
60. Partial Link Text for Product Details
<a href="/product/101">View Product Details</a>
driver.findElement(By.partialLinkText("Product Details")).click();
61. Partial Link Text with Page Object Model
Partial Link Text can be used inside a Page Object Model class.
public class HomePage {
WebDriver driver;
By seleniumCourseLink =
By.partialLinkText("Selenium");
public HomePage(WebDriver driver) {
this.driver = driver;
}
public void openSeleniumCourse() {
driver.findElement(seleniumCourseLink).click();
}
}
62. Reusable Partial Link Text Method
public void clickPartialLink(String text) {
driver.findElement(By.partialLinkText(text)).click();
}
Usage:
clickPartialLink("Selenium");
63. Reusable Method with Explicit Wait
public void clickPartialLink(String text) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement link = wait.until(
ExpectedConditions.elementToBeClickable(
By.partialLinkText(text)
)
);
link.click();
}
64. Partial Link Text in TestNG
import org.testng.annotations.Test;
public class LinkTest extends BaseTest {
@Test
public void verifySeleniumLink() {
driver.findElement(
By.partialLinkText("Selenium")
).click();
}
}
65. Partial Link Text Test Validation
WebElement link = driver.findElement(
By.partialLinkText("Selenium")
);
Assert.assertTrue(link.isDisplayed());
66. Partial Link Text and NoSuchElementException
If Selenium cannot find a matching link, a NoSuchElementException can occur.
driver.findElement(By.partialLinkText("NonExistingLink")).click();
Possible reasons include:
- Incorrect partial text.
- Link is not present.
- Page has not loaded the link yet.
- Wrong page or URL.
- Link is inside an iframe.
- Text is different from the expected value.
67. Handling Dynamic Links with Explicit Wait
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement link = wait.until(
ExpectedConditions.presenceOfElementLocated(
By.partialLinkText("Reports")
)
);
link.click();
68. Partial Link Text and iframe
If the link is located inside an iframe, Selenium must switch into the iframe before locating the link.
driver.switchTo().frame("paymentFrame");
driver.findElement(
By.partialLinkText("Continue")
).click();
driver.switchTo().defaultContent();
69. Partial Link Text and Nested Frames
For nested frames, Selenium must switch through the appropriate frame hierarchy before locating the link.
driver.switchTo().frame("outerFrame");
driver.switchTo().frame("innerFrame");
driver.findElement(
By.partialLinkText("Continue")
).click();
driver.switchTo().defaultContent();
70. Partial Link Text and Shadow DOM
Partial Link Text does not directly solve Shadow DOM boundary issues. If a link is inside a shadow root, the shadow root must first be accessed using Selenium's Shadow DOM support and then the appropriate element locator can be used within that context.
71. Partial Link Text and AJAX Applications
AJAX-based applications can dynamically add links after the page initially loads. In such cases, explicit waits can help synchronize the test.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement link = wait.until(
ExpectedConditions.elementToBeClickable(
By.partialLinkText("Dashboard")
)
);
link.click();
72. Partial Link Text for Search Results
<a href="/result/1">Learn Selenium WebDriver Automation</a>
<a href="/result/2">Learn Java Programming</a>
driver.findElement(By.partialLinkText("Selenium WebDriver")).click();
73. Partial Link Text for Pagination
<a href="/page/2">Go to Next Page</a>
driver.findElement(By.partialLinkText("Next Page")).click();
74. Partial Link Text for Help Center
<a href="/help">Visit the Customer Help Center</a>
driver.findElement(By.partialLinkText("Help Center")).click();
75. Partial Link Text for Terms and Conditions
<a href="/terms">Read Terms and Conditions</a>
driver.findElement(By.partialLinkText("Terms")).click();
76. Partial Link Text for Privacy Policy
<a href="/privacy">Read Our Privacy Policy</a>
driver.findElement(By.partialLinkText("Privacy Policy")).click();
77. Common Mistake: Using Partial Text That Is Too Short
Consider:
<a href="/selenium">Selenium Training</a>
<a href="/selenium-advanced">Selenium Advanced Training</a>
This is too generic:
By.partialLinkText("Selenium")
A more specific partial text is:
By.partialLinkText("Selenium Advanced")
78. Common Mistake: Assuming Partial Link Text Works on Buttons
Partial Link Text is intended for anchor links. A button such as:
<button>Login</button>
should not be treated as a normal link-text locator. Use another suitable locator such as ID, CSS Selector, or XPath.
79. Common Mistake: Ignoring Duplicate Links
If multiple links contain the same partial text, verify the number of matching elements before clicking.
List<WebElement> links =
driver.findElements(By.partialLinkText("View"));
System.out.println(links.size());
80. Common Mistake: Depending on Frequently Changing Text
If a link text changes on every execution, Partial Link Text may become unreliable.
View Order 1001
View Order 1002
View Order 1003
A stable attribute-based CSS Selector or XPath may be a better option if available.
81. Common Mistake: Using Generic Words
Avoid partial values such as:
Click
View
More
Learn
Open
These words may occur in many links on the same page.
82. Best Practice: Use Stable Text
Choose a portion of the link text that is expected to remain stable.
driver.findElement(
By.partialLinkText("Selenium Automation")
).click();
83. Best Practice: Keep the Locator Specific
Prefer a unique partial phrase over a single common word.
By.partialLinkText("Automation Testing Course")
is generally more specific than:
By.partialLinkText("Course")
84. Best Practice: Inspect the DOM
Before creating a locator, inspect the HTML structure using browser Developer Tools. Check the actual anchor text, attributes, parent elements, and whether multiple matching links exist.
85. Best Practice: Prefer Stable Attributes When Available
If a unique and stable ID exists, it may be preferable to relying on visible text.
<a id="selenium-course" href="/selenium">Learn Selenium</a>
driver.findElement(By.id("selenium-course")).click();
86. Best Practice: Use Explicit Waits for Dynamic Pages
When links appear asynchronously, use an appropriate explicit wait rather than relying on arbitrary sleep statements.
87. Best Practice: Avoid Thread.sleep()
Using fixed delays can slow tests and still fail when application response time changes.
Prefer:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement link = wait.until(
ExpectedConditions.elementToBeClickable(
By.partialLinkText("Login")
)
);
88. Best Practice: Use Page Object Model
Store frequently used Partial Link Text locators inside page classes instead of scattering locator strings throughout test cases.
private By coursesLink =
By.partialLinkText("Selenium Courses");
89. Real-World Login Navigation Example
public void openLoginPage() {
driver.findElement(
By.partialLinkText("Login")
).click();
}
90. Real-World Registration Navigation Example
public void openRegistrationPage() {
driver.findElement(
By.partialLinkText("Create Account")
).click();
}
91. Real-World Course Navigation Example
public void openSeleniumCourse() {
driver.findElement(
By.partialLinkText("Selenium Training")
).click();
}
92. Real-World E-Commerce Navigation Example
public void openProductDetails() {
driver.findElement(
By.partialLinkText("Product Details")
).click();
}
93. Real-World Banking Navigation Example
public void openTransactions() {
driver.findElement(
By.partialLinkText("Transactions")
).click();
}
94. Real-World Admin Dashboard Example
public void openUserManagement() {
driver.findElement(
By.partialLinkText("User Management")
).click();
}
95. Complete Partial Link Text Example
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class PartialLinkTextDemo {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
driver.manage().window().maximize();
driver.get("https://example.com");
WebDriverWait wait =
new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement link = wait.until(
ExpectedConditions.elementToBeClickable(
By.partialLinkText("Selenium")
)
);
System.out.println("Link text: " + link.getText());
link.click();
} finally {
driver.quit();
}
}
}
96. Partial Link Text Locator Checklist
- Confirm that the target element is an anchor link.
- Inspect the visible text of the link.
- Choose a stable portion of the text.
- Make sure the partial text is sufficiently unique.
- Check whether multiple matching links exist.
- Use explicit waits for dynamically loaded links.
- Switch into an iframe before locating links inside it.
- Prefer stable attributes when they provide a clearer locator.
- Keep locators maintainable.
- Validate the locator before using it in multiple tests.
97. Interview Question: What is Partial Link Text?
Partial Link Text is a Selenium locator strategy used to locate an anchor element by matching a portion of its visible link text. In Java, it is implemented using By.partialLinkText().
98. Interview Question: What is the Syntax?
driver.findElement(By.partialLinkText("Login")).click();
99. Interview Question: Difference Between Link Text and Partial Link Text?
Link Text generally identifies a link using its complete visible text, whereas Partial Link Text identifies it using part of its visible text.
100. Interview Question: Can Partial Link Text Locate a Button?
Partial Link Text is intended for anchor links. For buttons, use a suitable locator such as ID, Name, Class Name, CSS Selector, or XPath.
101. Interview Question: What Happens When Multiple Links Match?
If multiple links match the partial text, using findElement() can result in Selenium interacting with the first matching element according to WebDriver's element search behavior. To inspect all matches, use findElements().
102. Interview Question: Can Partial Link Text Handle Dynamic Links?
Yes, when a stable portion of the link text remains unchanged. For highly dynamic text, an attribute-based CSS Selector or XPath may provide a more stable locator.
103. Interview Question: Can Partial Link Text Be Used with Explicit Wait?
Yes. The same By.partialLinkText() locator can be passed to Selenium's expected conditions.
wait.until(
ExpectedConditions.elementToBeClickable(
By.partialLinkText("Login")
)
);
104. Interview Question: When Should You Avoid Partial Link Text?
- When the partial text is not unique.
- When the text changes frequently.
- When a better stable locator is available.
- When the target is not an anchor element.
- When a complex relationship-based locator is required.
105. Partial Link Text vs All Selenium Locators
| Locator | Method | Typical Usage |
| ID | By.id() | Unique id attribute |
| Name | By.name() | name attribute |
| Class Name | By.className() | class attribute |
| Tag Name | By.tagName() | HTML tag |
| Link Text | By.linkText() | Complete visible link text |
| Partial Link Text | By.partialLinkText() | Part of visible link text |
| CSS Selector | By.cssSelector() | CSS-based identification |
| XPath | By.xpath() | Advanced element identification |
106. Practical Example: Course Website
<nav>
<a href="/courses">Explore Selenium Automation Courses</a>
<a href="/demo">Register for Course Demo</a>
</nav>
Automation examples:
driver.findElement(
By.partialLinkText("Selenium Automation")
).click();
driver.findElement(
By.partialLinkText("Course Demo")
).click();
107. Practical Example: User Account
<a href="/account">Manage Your Account Settings</a>
driver.findElement(
By.partialLinkText("Account Settings")
).click();
108. Practical Example: Help Page
<a href="/help">Visit Our Customer Support Center</a>
driver.findElement(
By.partialLinkText("Customer Support")
).click();
109. Practical Example: Order Tracking
<a href="/orders/123">Track Your Order Status</a>
driver.findElement(
By.partialLinkText("Order Status")
).click();
110. Practical Example: Download Link
<a href="/download/report.pdf">Download Monthly Sales Report</a>
driver.findElement(
By.partialLinkText("Sales Report")
).click();
111. Practical Example: Documentation Link
<a href="/docs">Read Selenium Documentation</a>
driver.findElement(
By.partialLinkText("Selenium Documentation")
).click();
112. Debugging Partial Link Text
When a Partial Link Text locator does not work, check the following:
- Verify the exact visible text in Developer Tools.
- Confirm the element is an
<a> element.
- Check whether the text is generated dynamically.
- Check whether multiple links contain the same partial text.
- Check whether the element is inside an iframe.
- Wait for the element if the page loads it asynchronously.
- Try an XPath or CSS Selector when a more specific locator is required.
113. Common Selenium Exceptions Related to Partial Link Text
| Exception | Possible Cause | Possible Solution |
| NoSuchElementException | Link not found | Check text, page, iframe, and timing |
| ElementNotInteractableException | Element cannot be interacted with | Wait or verify visibility |
| ElementClickInterceptedException | Another element blocks the link | Wait, scroll, or handle overlay |
| StaleElementReferenceException | DOM changed after locating element | Locate the element again |
| TimeoutException | Expected condition not satisfied | Review wait condition and locator |
114. Recommended Locator Selection Flow
Need to locate a link
↓
Is there a unique stable ID?
↓
Yes → Use ID
↓
No
↓
Is complete link text stable and unique?
↓
Yes → Use Link Text
↓
No
↓
Is a stable unique partial text available?
↓
Yes → Use Partial Link Text
↓
No
↓
Can CSS Selector identify it clearly?
↓
Yes → Use CSS Selector
↓
No
↓
Use XPath
115. Advantages of Partial Link Text
- Simple syntax.
- Easy to learn.
- Useful for long link text.
- Useful when only a stable portion of text is known.
- Readable in test scripts.
- Convenient for navigation links.
116. Limitations of Partial Link Text
- Primarily intended for anchor links.
- Can become ambiguous when many links contain the same text.
- Can become unstable when visible text changes frequently.
- It provides less flexibility than XPath for complex relationships.
- It may not be the best choice when stable attributes are available.
117. Best Practices Summary
- Use Partial Link Text when the link text contains a unique stable phrase.
- Avoid very short or generic partial values.
- Use
findElements() when you need to check multiple matches.
- Use explicit waits for dynamically loaded links.
- Inspect the DOM before creating locators.
- Use stable IDs or attributes when they are available.
- Use XPath or CSS Selector when more precise identification is necessary.
- Keep locators readable and maintainable.
- Store reusable locators in Page Object classes.
- Validate important locators as part of the test design.
118. Quick Reference
| Requirement | Example |
| Complete link text | By.linkText("Login to Account") |
| Partial link text | By.partialLinkText("Login") |
| Click link | driver.findElement(By.partialLinkText("Login")).click() |
| Get link text | element.getText() |
| Check visibility | element.isDisplayed() |
| Find multiple links | driver.findElements(By.partialLinkText("Course")) |
| Wait for link | ExpectedConditions.elementToBeClickable(By.partialLinkText("Login")) |
119. Mini Project: Automating a Course Website
Suppose a course website contains the following navigation links:
<a href="/courses">Explore Automation Courses</a>
<a href="/selenium">Learn Selenium Automation</a>
<a href="/demo">Register for Course Demo</a>
<a href="/contact">Contact Training Team</a>
The Selenium script can interact with these links using Partial Link Text:
driver.findElement(
By.partialLinkText("Selenium Automation")
).click();
After returning to the appropriate page:
driver.findElement(
By.partialLinkText("Course Demo")
).click();
120. Mini Project Test Flow
Launch Browser
↓
Open Course Website
↓
Locate Selenium Link
↓
Use Partial Link Text
↓
Click Link
↓
Verify Destination Page
↓
Return to Course Page
↓
Locate Demo Link
↓
Click Demo Link
↓
Verify Registration Page
↓
Close Browser
121. Complete Concept Summary
Partial Link Text is a Selenium locator strategy that identifies anchor links using a portion of their visible text. In Java, the primary syntax is By.partialLinkText("text"). It is particularly useful for long link names where a stable and sufficiently unique portion of the text can identify the desired link.
For reliable automation, the selected partial text should be unique, stable, and meaningful. When multiple links match, dynamic content makes the locator unstable, or more complex identification is required, other locators such as CSS Selector or XPath may be more appropriate.
122. Final Practical Example
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class FinalPartialLinkTextExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
driver.manage().window().maximize();
driver.get("https://example.com");
WebDriverWait wait =
new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement courseLink = wait.until(
ExpectedConditions.elementToBeClickable(
By.partialLinkText("Selenium")
)
);
System.out.println(
"Found Link: " + courseLink.getText()
);
courseLink.click();
} finally {
driver.quit();
}
}
}
123. Interview Preparation Checklist
- Understand what Partial Link Text means.
- Know the syntax of
By.partialLinkText().
- Understand the difference between Link Text and Partial Link Text.
- Know that it is intended for links.
- Understand why unique partial text is important.
- Know how to use Partial Link Text with
findElement().
- Know how to use it with
findElements().
- Understand dynamic link problems.
- Know how explicit waits can be combined with Partial Link Text.
- Understand when XPath or CSS Selector may be more appropriate.
124. Key Points to Remember
- Method:
By.partialLinkText()
- Purpose: Locate links using a portion of visible text.
- Primary element:
<a>
- Common action:
click()
- Important requirement: Partial text should be sufficiently unique.
- Dynamic pages: Use explicit waits when required.
- Multiple matches: Use
findElements() to inspect them.
- Complex scenarios: Consider CSS Selector or XPath.
125. Conclusion
Partial Link Text is a simple and useful Selenium locator for identifying hyperlinks when only a portion of their visible text is known. It is especially convenient for long navigation links, course links, account links, help links, product links, and other anchor elements.
For reliable automation, always choose a meaningful and unique portion of the link text instead of using generic words. When link text is dynamic or multiple elements contain the same phrase, consider using stable attributes, CSS Selectors, or XPath.
To learn more about Selenium automation testing and locator strategies, explore the JustAcademy's Selenium Training Course.
You can also explore the available learning/demo option through the Register for Course Demo link.