Introduction
The Partial Link Text Locator is one of the basic locator strategies in Selenium WebDriver. It identifies hyperlink (<a>) elements using a portion of their visible text instead of requiring the complete link text.
This locator is useful when the full link text is long, dynamic, or only partially known. Selenium searches for hyperlinks whose visible text contains the specified partial text.
The Partial Link Text locator provides greater flexibility than the Link Text locator but should be used carefully because multiple links may contain the same partial text.
In this tutorial, you will learn what the Partial Link Text Locator is, why it is used, its syntax, practical examples, real-world applications, common mistakes, best practices, and frequently asked interview questions.
What is a Partial Link Text Locator?
The Partial Link Text Locator is a Selenium locator strategy that identifies hyperlink (<a>) elements using part of their visible text.
For example, consider the following HTML:
<a href="/login">Form Authentication</a>
The hyperlink can be located using:
driver.find_element(By.PARTIAL_LINK_TEXT, "Form Auth")
Since “Form Auth” is part of the visible link text “Form Authentication”, Selenium successfully locates the hyperlink.
Why Do We Use the Partial Link Text Locator?
The Partial Link Text Locator is commonly used because it:
Locates hyperlinks using only part of their visible text.
Is useful when the complete link text is very long.
Makes automation scripts more flexible.
Works well with dynamic link text.
Eliminates the need to remember the entire visible text.
Syntax
driver.find_element(By.PARTIAL_LINK_TEXT, "Partial Text")
Where:
By.PARTIAL_LINK_TEXT specifies that Selenium should locate the hyperlink using a portion of its visible text.
Partial Text is any part of the displayed hyperlink text.
Practical Example
The following example demonstrates how to locate a hyperlink using Partial Link Text.
The automation script opens the Selenium practice homepage, locates the “Form Authentication” hyperlink using only the partial text “Form Auth”, and verifies that it navigates to the login page.
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: 11. Basic Locators - Partial Link Text
# Practice site: https://the-internet.herokuapp.com/
#
# By.PARTIAL_LINK_TEXT locates anchor elements when only part of the link
# text is known.
def test_find_element_by_partial_link_text():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/")
link = driver.find_element(By.PARTIAL_LINK_TEXT, "Form Auth")
assert link.get_attribute("href").endswith("/login")
finally:
driver.quit()
Output
Browser launched successfully.
Homepage opened successfully.
Hyperlink located using Partial Link Text.
Matching Link:
Form Authentication
Link URL:
https://the-internet.herokuapp.com/login
Assertion Passed.
Test Executed Successfully.
Understanding the Code
Import Required Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
Imports Selenium WebDriver and the By class required for locating web elements.
Launch Chrome Browser
driver = webdriver.Chrome()
Starts a new Chrome browser session.
Open the Homepage
driver.get("https://the-internet.herokuapp.com/")
Navigates to the Selenium practice homepage.
Locate the Hyperlink Using Partial Link Text
link = driver.find_element(By.PARTIAL_LINK_TEXT, "Form Auth")
Locates the hyperlink whose visible text contains “Form Auth”.
Validate the Link
assert link.get_attribute("href").endswith("/login")
Verifies that the hyperlink points to the login page.
If the assertion passes, Selenium has successfully located the correct hyperlink.
Close the Browser
driver.quit()
Closes all browser windows and ends the WebDriver session.
Execution Flow
Launch Browser
│
▼
Open Homepage
│
▼
Locate Hyperlink Using Partial Link Text
│
▼
Read Link URL
│
▼
Verify Destination URL
│
▼
Assertion Passes
│
▼
Close Browser
Automation Testing Example
Suppose an employee portal contains the following hyperlink:
<a href="/employee/login">Employee Login Portal</a>
Instead of using the complete text, you can locate it using:
driver.find_element(By.PARTIAL_LINK_TEXT, "Employee Login")
This makes the automation script shorter and easier to maintain.
Real-World Example
Consider an e-commerce website containing:
<a href="/orders">View My Orders</a>
The hyperlink can be located using:
driver.find_element(By.PARTIAL_LINK_TEXT, "My Orders")
This is useful when only part of the displayed text is known.
Common Mistakes Beginners Make
Using Partial Text That Matches Multiple Links
If multiple hyperlinks contain the same partial text, Selenium returns the first matching element, which may not be the intended one.
Example
<a>View Orders</a>
<a>View Order History</a>
Using:
driver.find_element(By.PARTIAL_LINK_TEXT, "View")
may return the wrong hyperlink.
Use a more specific partial text whenever possible.
Using Partial Link Text on Non-Link Elements
Incorrect
driver.find_element(By.PARTIAL_LINK_TEXT, "Login")
when the element is actually:
<button>Login</button>
The Partial Link Text locator works only with hyperlinks (<a> elements).
Using Very Short Partial Text
Using extremely short values such as:
driver.find_element(By.PARTIAL_LINK_TEXT, "Fo")
may match multiple links and produce unreliable results.
Best Practices
Use Partial Link Text only for hyperlinks.
Choose a partial text that uniquely identifies the hyperlink.
Prefer Link Text when the complete visible text is stable and easy to use.
Avoid using very short partial strings.
Verify that only one hyperlink matches the selected partial text.
Conclusion
The Partial Link Text Locator allows Selenium to locate hyperlinks using only a portion of their visible text. It is particularly useful when the full link text is long, dynamic, or partially known.
Although it provides greater flexibility than the Link Text locator, it should be used carefully to avoid matching unintended hyperlinks.
Understanding the Partial Link Text Locator helps automation engineers build more flexible and maintainable Selenium automation scripts.
Frequently Asked Questions (FAQs)
What is the Partial Link Text Locator in Selenium?
The Partial Link Text Locator identifies hyperlinks using part of their visible text.
What is the syntax for the Partial Link Text Locator?
driver.find_element(By.PARTIAL_LINK_TEXT, "Partial Text")
What is the difference between Link Text and Partial Link Text?
Link Text requires the exact visible text.
Partial Link Text requires only a portion of the visible text.
Can Partial Link Text be used for buttons?
No.
It works only with HTML hyperlink (<a>) elements.
When should I use Partial Link Text?
Use it when the hyperlink text is long, dynamic, or only partially known.
Key Takeaways
The Partial Link Text Locator identifies hyperlinks using part of their visible text.
The syntax is
driver.find_element(By.PARTIAL_LINK_TEXT, "Partial Text").It works only with HTML
<a>elements.It is useful when the complete link text is long or dynamic.
Use meaningful partial text that uniquely identifies the hyperlink.
Prefer Link Text when the complete visible text is short and stable.
The Partial Link Text Locator is a commonly used Selenium locator and an important interview topic.
