How to Use By.linkText() & By.partialLinkText() in Selenium WebDriver

In Selenium WebDriver, By.linkText() and By.partialLinkText() are used to locate anchor tags (<a>) with specific text.

🔎 What is By.linkText()?

By.linkText() is used when the exact text of a link is known. It matches the link exactly as written in the HTML.

<a href="https://example.com/login">Login</a>

✅ When to Use By.linkText()?

📌 Syntax in Java

driver.findElement(By.linkText("exact_link_text"));

💻 Example: Clicking on a Link with Exact Text

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class LinkTextExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

        driver.get("https://example.com");

        // Locate link with exact text
        WebElement loginLink = driver.findElement(By.linkText("Login"));

        // Click the link
        loginLink.click();

        driver.quit();
    }
}

🔎 What is By.partialLinkText()?

By.partialLinkText() is used when you want to match only a part of the link text. This locator is useful when the full link text is dynamic or when you only know part of the text.

<a href="https://example.com/login">Login Page</a>

✅ When to Use By.partialLinkText()?

📌 Syntax in Java

driver.findElement(By.partialLinkText("partial_link_text"));

💻 Example: Clicking on a Link with Partial Text

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class PartialLinkTextExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

        driver.get("https://example.com");

        // Locate link with partial text
        WebElement loginLink = driver.findElement(By.partialLinkText("Login"));

        // Click the link
        loginLink.click();

        driver.quit();
    }
}

✅ Best Practices

Locator When to Use Pros Cons
By.linkText() When the exact link text is known Easy to implement, precise Breaks if the link text changes
By.partialLinkText() When you only know part of the link text Flexible, useful for dynamic content Not always precise if too many links share similar text

⚠️ Common Mistakes

Mistake Fix
Using By.linkText() when link text is not unique Use other locators like By.id() or By.cssSelector() if possible
Using By.partialLinkText() with vague text Ensure partial text is specific enough to avoid matching multiple links

📚 Summary

  • By.linkText() is used for exact matches with link text.
  • By.partialLinkText() is used when you know part of the link text.
  • Use By.linkText() for static content and By.partialLinkText() for dynamic or partial content.

🚀 Keep Learning

Explore other locators in Selenium:

🤖
PrepCampusPlus AI Tutor
Scroll to Top