Contents

    Guides

    Link Text in Selenium: How to Use Link & Partial

    Published on

    October 29, 2025
    Link Text in Selenium: How to Use Link & Partial

    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.

    What is “Link Text” in Selenium?

    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:

    • It matches the full, visible text of an <a> element.
    • It provides clarity and simplicity in automation scripts.
    • It’s best suited when the link’s label doesn’t change frequently.

    Syntax examples

    • Java: 

    driver.findElement(By.linkText("Exact Link Text")).click();

    • Python: 

    driver.find_element(By.LINK_TEXT, "Exact Link Text").click()

    When to use it

    • The full text of the link is static and known beforehand.
    • You want your test code to be human-readable and explicit.
    • You need precise control over link selection when duplicates are rare.

    What is “Partial Link Text” in Selenium?

    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:

    • It matches elements containing a specific portion of text.
    • It helps when the complete text is too long or partially dynamic.
    • It can improve test resilience when small changes occur in link wording.

    Syntax examples

    • Java: 

    driver.findElement(By.partialLinkText("PartOfText")).click();

    • Python: 

    driver.find_element(By.PARTIAL_LINK_TEXT, "PartOfText").click()

    When to use it

    • Only part of the text is stable or unique.
    • The link text includes variable segments like prices or usernames.
    • The application UI frequently updates link phrasing.

    Step-by-Step: Using Link Text and Partial Link Text

    To use these locators effectively, follow this general workflow when writing your Selenium test scripts:

    1. Set up WebDriver by importing the appropriate libraries and initializing the browser driver.
    2. Navigate to the target page using driver.get("https://example.com").
    3. Wait for elements to load using explicit or implicit waits.
    4. Locate and interact with links using either Link Text or Partial Link Text.

    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()

    Link Text vs Partial Link Text — Key Differences

    Before choosing between these two locators, consider the main differences that affect test precision and flexibility:

    Feature Link Text Partial Link Text
    Matching Exact visible text match Visible text contains the substring
    Flexibility Rigid More adaptable
    Use case Stable, short link labels Dynamic or lengthy link labels
    Risk of false match Lower Higher if substring repeats
    Readability Very high Slightly lower
    Performance Slightly faster Marginally slower
    Ideal for Predictable link text Evolving UI text

    Common Pitfalls in Selenium Link text and How to Avoid Them

    When using Link Text and Partial Link Text, keep an eye on these common mistakes:

    • Non-anchor elements: These locators only work on <a> tags.
    • Whitespace or case differences: Matching fails if spacing or capitalization differs.
    • Duplicate text links: Only the first match is returned.
    • Overly generic substrings: Using short text like “Click” may target multiple elements.
    • Dynamic or translated text: Fixed text locators may break in multilingual sites.
    • Timing issues: If elements are not yet loaded, exceptions may occur—use waits.

    Best Practices for Using Link Text Locators

    Follow these practices to ensure reliable and maintainable Selenium tests:

    • Verify that each link’s text is unique within the page.
    • Prefer Link Text for stable, short, and unique link labels.
    • Choose Partial Link Text when only part of the text is consistent.
    • Combine link text locators with other strategies like XPath or CSS for complex DOMs.
    • Use explicit waits to ensure the element is visible and clickable.
    • Avoid using text-based locators in highly dynamic or multi-language environments.

    When to Consider Other Locators

    Sometimes, alternative locator strategies can be more appropriate than Link Text or Partial Link Text. Consider switching when:

    • The element isn’t a true hyperlink or has no visible text.
    • The link text frequently changes with updates or localization.
    • Multiple identical links exist on the same page.
    • You need to identify links using attributes like href, id, or custom data attributes.

    In such cases, use By.id, By.cssSelector, or By.xpath for better precision and stability.

    Testing Selenium on Real Devices

    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:

    • Access Real Devices and Browsers: Test on a variety of real devices (iOS, Android) and desktop browsers (Chrome, Firefox, Safari, Edge).
    • Cross-Browser and Cross-Platform Testing: Run tests across multiple browsers and platforms to ensure broad compatibility.
    • No Infrastructure Required: Save time and resources by testing on real devices hosted on BrowserStack’s cloud.
    • Integration with CI/CD: Easily integrate with Jenkins, CircleCI, and GitHub Actions to automate testing in your CI/CD pipeline.
    • Instant Feedback with Live Debugging: Get real-time logs, videos, and easy debugging of failed tests.
    • Easy to Set Up and Use: Quickly set up and start running tests without complex configurations.

    Summary

    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

    Data-rich bug reports loved by everyone

    Get visual proof, steps to reproduce and technical logs with one click

    Make bug reporting 50% faster and 100% less painful

    Rating LogosStars
    4.6
    |
    Category leader

    Liked the article? Spread the word

    Put your knowledge to practice

    Try Bird on your next bug - you’ll love it

    “Game changer”

    Julie, Head of QA

    star-ratingstar-ratingstar-ratingstar-ratingstar-rating

    Overall rating: 4.7/5

    Try Bird later, from your desktop