Link Text and Partial Link Text Locators in Selenium

Locating elements is a fundamental part of automating web applications with Selenium WebDriver. Among the various locator strategies Selenium offers — such as ID, class name, name, CSS selector, and XPath — two are specifically useful for hyperlinks: link text and partial link text. These locators are designed to target anchor (<a>) tags based on the visible text of the link.

In this blog, we’ll explore how linkText and partialLinkText locators work, when to use them, their advantages and limitations, and best practices for reliable element targeting.


What Are Link Text and Partial Link Text Locators?

linkText: Locates an anchor element by matching the exact visible text of the hyperlink.

partialLinkText: Locates an anchor element by matching a portion of the visible text.

These are particularly useful when interacting with navigation menus, footers, sidebars, or any part of the UI where links are present.


Syntax

Here’s how you use these locators in Selenium with Java:


java

CopyEdit

// Using linkText to click a link with exact text

driver.findElement(By.linkText("Contact Us")).click();


// Using partialLinkText to click a link containing part of the text

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

Both methods return a WebElement object, which you can interact with using methods like .click(), .getText(), .isDisplayed(), etc.


Practical Example

Consider the following HTML snippet:


html

CopyEdit

<a href="/about">About Us</a>

<a href="/contact">Contact Us</a>

<a href="/services">Our Services</a>

Using linkText:

java

CopyEdit

driver.findElement(By.linkText("Contact Us")).click();  // Works

driver.findElement(By.linkText("Contact"));  // Throws NoSuchElementException

Using partialLinkText:

java

CopyEdit

driver.findElement(By.partialLinkText("Contact")).click();  // Works

driver.findElement(By.partialLinkText("Us")).click();  // Also works


When to Use Link Text Locators

  • When link text is unique and consistent.
  • For applications where link structure doesn't change frequently.
  • When working with static websites or predefined navigation links.


When to Use Partial Link Text Locators

  • When link text is long or contains dynamic prefixes/suffixes.
  • When only part of the link text is known.


For searching and filtering link-based content, especially in test automation scripts that run across multiple environments.


Advantages

  • Readable and simple: These locators closely match how humans read web pages.
  • No complex selectors needed: Especially useful for non-technical testers.

Limitations

  • Only works with <a> tags: These locators can't be used on buttons or other elements.
  • Case-sensitive: "Contact Us" and "contact us" are treated differently.
  • Brittle in dynamic UIs: If link text changes due to localization or content updates, tests may fail.


Best Practices

  • Avoid overusing partialLinkText: It can match multiple elements, leading to ambiguous behavior.
  • Prefer linkText when the text is short and unique: More precise and less prone to errors.
  • Combine with waits: Always use explicit waits like WebDriverWait to ensure elements are loaded before interaction.
  • Use descriptive and meaningful text: Helps both users and automated tests.


Conclusion


linkText and partialLinkText locators are simple, human-readable ways to interact with hyperlinks in Selenium WebDriver. They are best suited for static, predictable UIs and can make your test scripts more intuitive. However, for dynamic applications or non-unique links, combining these locators with other strategies (like XPath or CSS selectors) often results in more robust and reliable automation.

 Learn  Selenium with Python Training Course

Read More: CSS Selectors in Selenium with Python

Visit Quality Thought Training Institute in Hyderabad
Get Direction


Comments

Popular posts from this blog

Tosca vs Selenium: Which One to Choose?

Flask API Optimization: Using Content Delivery Networks (CDNs)

Using ID and Name Locators in Selenium Python