Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Selenium Python, use By.LINK_TEXT to locate a link by its exact displayed text, or By.PARTIAL_LINK_TEXT to locate a link whose text contains a specified fragment. Pass either locator to find_element for the first match or find_elements for all matches.

Use the right link-text locator

These strategies are for links, not general-purpose text searches across every kind of page element. Selenium’s locator protocol describes them as matching anchor elements by visible text. The current Python API reference, for Selenium 4.49.0, documents the two locator constants and their use with find_element.

Locator What it matches When to use it
By.LINK_TEXT A link whose text matches the supplied text exactly. Use when you know the full, stable label, such as Documentation.
By.PARTIAL_LINK_TEXT A link whose text contains the supplied text fragment. Use when a distinctive part of the label is sufficient, such as Doc.

For the documented definitions, see the Selenium Python By reference and the Selenium locator protocol reference.

Find one link or collect every match

Import By from selenium.webdriver.common.by, then pass the locator and text to the driver. The example uses a full label for the exact locator and a fragment for the partial locator:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from selenium.webdriver.common.by import By

exact_link = driver.find_element(By.LINK_TEXT, "Documentation")
partial_link = driver.find_element(By.PARTIAL_LINK_TEXT, "Doc")
all_matching_links = driver.find_elements(By.PARTIAL_LINK_TEXT, "Doc")

find_element returns the first matching WebElement. It does not establish that the locator matched only one link. find_elements returns a list of matching elements, which lets you inspect the result count. The Selenium Python WebDriver API reference documents these single- and multiple-result methods.

Choose a phrase that identifies the intended link

Prefer exact text when the full label is known

An exact locator communicates the intended label clearly and avoids matching a different link merely because it shares a short fragment. It is a good choice when the displayed wording is stable.

Make partial text distinctive

A common fragment can occur in several link labels. Use a phrase specific enough for the target; if duplicates are plausible, call find_elements and check the number of results rather than assuming the first result is unique.

Use a different strategy for non-link elements

If the target is a heading, button, or other non-link element, link-text locators are not general text locators. Choose a locator strategy suited to that element and the page structure.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use the method names for your Selenium language

The examples above are Python syntax. In Java, the corresponding methods are By.linkText(...) and By.partialLinkText(...); Python constants such as By.LINK_TEXT are not Java syntax. See the Selenium Java By API.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.