
When automating web applications with Selenium WebDriver, selecting the right locator strategy determines how reliable and maintainable your tests will be. Among the most useful options for identifying hyperlinks are Link Text and Partial Link Text. Both help you locate anchor (<a>) elements based on visible text. Let’s explore what these locators do, how to use them effectively, and the best practices to follow.
The Link Text locator is used to find a hyperlink whose visible text exactly matches the provided string. It is ideal when the link text is predictable and unique on the page.
Here’s what you should know about using Link Text:
Syntax examples
driver.findElement(By.linkText("Exact Link Text")).click();
driver.find_element(By.LINK_TEXT, "Exact Link Text").click()
When to use it
The Partial Link Text locator provides more flexibility by allowing matches based on a substring of the visible text. This is especially useful when dealing with long, dynamic, or changing link labels.
Here’s why you might prefer Partial Link Text:
Syntax examples
driver.findElement(By.partialLinkText("PartOfText")).click();
driver.find_element(By.PARTIAL_LINK_TEXT, "PartOfText").click()
When to use it
To use these locators effectively, follow this general workflow when writing your Selenium test scripts:
Example: Using Link Text
WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
WebElement link = driver.findElement(By.linkText("Sign Up"));
link.click();
driver = webdriver.Chrome()
driver.get("https://www.example.com")
link = driver.find_element(By.LINK_TEXT, "Sign Up")
link.click()
Example: Using Partial Link Text
WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
WebElement link = driver.findElement(By.partialLinkText("Sign"));
link.click();
driver = webdriver.Chrome()
driver.get("https://www.example.com")
link = driver.find_element(By.PARTIAL_LINK_TEXT, "Sign")
link.click()
Before choosing between these two locators, consider the main differences that affect test precision and flexibility:
When using Link Text and Partial Link Text, keep an eye on these common mistakes:
Follow these practices to ensure reliable and maintainable Selenium tests:

Sometimes, alternative locator strategies can be more appropriate than Link Text or Partial Link Text. Consider switching when:
In such cases, use By.id, By.cssSelector, or By.xpath for better precision and stability.
To make the process of testing on real devices seamless, BrowserStack Automate offers a robust cloud-based platform that allows you to run Selenium tests across real devices and browsers without the need for managing any infrastructure.
Here’s why BrowserStack Automate is the ideal solution for running your Selenium tests on real devices:
Using Link Text and Partial Link Text in Selenium provides a simple, human-readable way to locate hyperlinks on web pages. The main difference lies in how strictly they match the link’s visible text—exact versus partial.
By applying these locators strategically, avoiding common pitfalls, and adhering to best practices, you can make your Selenium scripts more robust and easier to maintain.
Run Selenium Tests on Cloud
Get visual proof, steps to reproduce and technical logs with one click
Continue reading
Try Bird on your next bug - you’ll love it
“Game changer”
Julie, Head of QA
Try Bird later, from your desktop