Partial Link Text in Selenium: When and How to Use It Effectively

Are you trying to locate a hyperlink by linkText, but it doesn't work in Selenium automation? You’re not alone.

If the text of the link is changing dynamically, then linkText doesn't work as it will look for the exact text of the hyperlink.

Here is the solution for this problem. 

You can use partial link text instead of the linkText element locator to locate such dynamic generated hyperlinks.

Use of partial link text in selenium

This article will teach you what partial link text is, how and when to use partial link text in Selenium automation.

What is partial link text in Selenium WebDriver?

ByPartialLinkText is a static inner class inside the By class.

It extends By and implements the locator strategy to find elements by partial link text.

To understand it easily, the partialLinkText is a locator strategy in Selenium WebDriver that allows you to locate anchor elements (<a> tags) by matching a part of the visible text of the hyperlink. that's it.

Why Use partialLinkText?

Using partial link text, you do not need full link text to identify the element.

I use it when the link text is too longthe text changes dynamically, or part of the text is predictable.

Another reason why I am using it is that it makes my tests more flexible.

How It Works

When you use it in an automation test, Selenium will scan the DOM for <a> elements. It will check for visible text (between <a> and </a>) that contains the substring that you have specified in code.

For example, consider the following HTML of a hyperlink.

<a href="https://example.com">Learn Selenium with Examples</a>

If you are working with Java Selenium, you can locate this link using:

WebElement link = driver.findElement(By.partialLinkText("Learn Selenium"));

or

WebElement link = driver.findElement(By.partialLinkText("with Examples"));

PartialLinkText syntax for Python Selenium is:

link = driver.find_element(By.PARTIAL_LINK_TEXT, "with Examples")

and for C# is:

IWebElement link = driver.FindElement(By.PartialLinkText("with Examples"));

Let me clarify that it works with anchor tags (<a>) only. So, do not try to locate other web elements like button, <button>, textbox, <input>, etc. using partial link text. It will not work with them.

Is it clear now? I think so.

Now let's dive into the examples..

Using PartialLinkText In Selenium: Step by Step

If you are a beginner, this step-by-step guide will help you understand how to use partial link text practically.

Step 1: Set up and configure Selenium in Eclipse

This guide will help you to understand how to download and install Selenium WebDriver in Eclipse IDE.

Step 2: Download and install Chrome Driver

I have already written a step-by-step tutorial on how to download and install Chrome Driver. You can refer to it.

Step 3: Create a project & write a code in Eclipse

You can create a new project and write the following code in the class file.

Before running this code, remember to replace the Chrome driver path, the test URL, and the link text with your actual one.

Java Example Code

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

        // Set the path to your ChromeDriver executable if necessary
        // System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize ChromeDriver (Selenium 4 style)
        WebDriver driver = new ChromeDriver();

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

        // Locate the <a> element using partialLinkText
        WebElement link = driver.findElement(By.partialLinkText("Learn"));

        // Click the link
        link.click();

        // Close the browser
        driver.quit();
    }
}

Step 4: Run the code

When you run the above code, it will open the Chrome browser, navigate to your given URL, locate an element by partial link text, click on it, and then close the browser.

Are you working in Python or C#? Don't worry. I have examples in both these languages as well.

Here they are:

Python Example Code

from selenium import webdriver
from selenium.webdriver.common.by import By

# Initialize the driver (Selenium 4 style)
driver = webdriver.Chrome()

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

# Find an <a> element containing "Learn" in its text
link = driver.find_element(By.PARTIAL_LINK_TEXT, "Learn")

# Perform click action
link.click()

# Close the driver
driver.quit()

C# Example Code

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

class Program
{
    static void Main(string[] args)
    {
        IWebDriver driver = new ChromeDriver();

        driver.Navigate().GoToUrl("https://example.com");

        // Locate <a> element with partial link text "Learn"
        IWebElement link = driver.FindElement(By.PartialLinkText("Learn"));

        // Click the link
        link.Click();

        // Close the browser
        driver.Quit();
    }
}

So, that's it on how to use partialLinkText in selenium.

But you should know how to use it efficiently, right?

Let's jump on best practices.

Best Practices for Using partialLinkText

Here are some important things to consider to prevent errors and improve test execution performance.

Use Unique Substrings

Always prefer using a unique substring. If text is changing frequently, your test will fail. This is a very very important point.

Avoid Common Words

We generally find common words like "Click" and "More" many times on the page. Avoid using such words to locate an element by partial link text.

Prefer linkText When You Know the Full Link Text

If text is unique and static, then you can use the linkText locator.

Interview Tip!

If the interviewer asks you a question, "When would you use partialLinkText over other locators?"

You can say: When I need to locate a link with long or dynamic visible text, and I know a unique part of that text, I use partialLinkText. It simplifies the locator and avoids brittle XPath expressions.

Winding Up

In summary, partialLinkText is a handy locator strategy in Selenium WebDriver when you need to interact with hyperlinks by matching only a portion of their visible text.

That's it. Any question or query? You can ask me your doubt by commenting below.

No comments:

Post a Comment