October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Find Elements by Link Text and Partial Link Text in Selenium

Use Selenium’s link-text locators to find anchors by their full visible label or a distinctive substring, and learn how to avoid ambiguous matches.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

Use Selenium’s By.LINK_TEXT to locate a link by its full visible text, or By.PARTIAL_LINK_TEXT when only a reliable phrase within the link is known. Both strategies target links—not buttons or arbitrary text—and a singular find_element returns the first match it finds.

Find a link by its visible text in Python

Import Selenium’s By class, then pass the locator strategy and the text to driver.find_element:

As an Amazon Associate I earn from qualifying purchases.

from selenium.webdriver.common.by import By

# Match the link using its full visible text.
link = driver.find_element(By.LINK_TEXT, "Selenium Official Page")

# Match a link whose visible text contains this phrase.
link = driver.find_element(By.PARTIAL_LINK_TEXT, "Official Page")

These examples assume driver is an already-created WebDriver and the page has loaded. Selenium’s locator documentation uses these strategies to find a link whose href is https://www.selenium.dev/; that is documentation example code, not a guarantee about another page’s content. See Selenium’s Locator strategies documentation for examples in Python, Java, JavaScript, C#, Ruby, and other bindings.

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

Choose exact text or partial text

Use exact link text when the label is stable

By.LINK_TEXT matches the anchor’s visible text as a whole. It is the more specific choice when the page reliably uses the same complete label. A changed label can make the locator stop matching.

Use partial link text when only a fragment is reliable

By.PARTIAL_LINK_TEXT matches an anchor when its visible text contains the supplied search string. This is useful when the rest of the label varies, but a broad fragment can match several links. Choose a distinctive substring where possible.

Remember that these strategies locate links only

Link-text strategies apply to anchor links, not any element that happens to display words. They do not locate buttons, headings, or arbitrary containers by their text. For a non-link element, choose a locator strategy suited to that element—for example, a stable ID or an appropriate CSS selector.

Handle duplicate matches safely

A call to find_element returns the first matching element in its search context. If more than one link has the same text or contains the same fragment, it can select a different link than intended without signaling that the match was ambiguous.

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

Use find_elements to inspect all matching links before choosing one:

matches = driver.find_elements(By.PARTIAL_LINK_TEXT, "Account")

for link in matches:
    print(link.text, link.get_attribute("href"))

Once you know which link you need, narrow the locator using a stable attribute or a more specific context rather than relying on a broad substring.

When to use an ID or CSS selector instead

Selenium’s official Tips on working with locators guidance says that unique, consistently predictable HTML IDs are generally preferred. If no suitable ID exists, Selenium recommends a well-written CSS selector. Keep locators compact and readable; avoid complex DOM traversal when a simpler locator identifies the element.

  • Prefer an ID when it is unique and predictably assigned.
  • Use exact link text when the full visible label is stable and distinctive.
  • Use partial link text when only a distinctive fragment remains stable.
  • Use CSS or another suitable strategy for non-links, or when it identifies the intended element more reliably.

Troubleshoot a link-text locator that does not work

No element is found

Confirm the element is a link and that the visible label or substring matches the current page. If the link is not yet present when the lookup runs, wait for the page state your test requires before locating it. Link text is not a locator for buttons or other non-anchor elements.

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

The wrong link is selected

Check whether the search fragment matches more than one link. Use find_elements to inspect candidates, then narrow the match with a more distinctive label, stable ID, CSS selector, or suitable search context.

The label changes between runs

Exact link text depends on the full visible label; partial link text still depends on its chosen fragment. If the text is dynamic or frequently revised, prefer a stable, unique ID or a well-written CSS selector when the page provides one.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If your goal is a screenshot rather than interacting with a link, ScreenshotNeo can capture a page with one request. Its API removes cookie/consent banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are not billed. An MCP server lets AI agents use screenshot tools, and the free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000.

See the ScreenshotNeo API documentation for request options. Example cURL request:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.