Skip to content
CodeKruPro

Match a Link with By.PARTIAL_LINK_TEXT

9/80

Match a Link with By.PARTIAL_LINK_TEXT

By.PARTIAL_LINK_TEXT matches a piece of the link text.

Task:

  1. Find the link whose text contains TextFields
  2. Find the link whose text contains Buttons
  3. Print each full link text

Print the lines below exactly.

Expected output:

Link 1: Practice TextFields
Link 2: Practice Buttons

About this lesson

Selenium Python By.PARTIAL_LINK_TEXT: match a link from part of its text

Selenium Python AssignmentsBeginnerLocators

How it works

Sometimes you know only a word from a link, or the full label is long and likely to change. By.PARTIAL_LINK_TEXT checks whether your string appears anywhere inside the visible text of an a element.

Two rules carry over from the exact version. The match is case sensitive, so a lower-case fragment of a capitalised word finds nothing and raises NoSuchElementException. And the strategy looks at visible text only. What differs is how many elements can match. A short fragment may fit several links, and find_element quietly returns the first in page order. On this page the word Practice appears in three labels, so a fragment that broad would give you the first of them and never tell you about the others. Choose a fragment unique enough for the link you want.

The full label is not what you passed in, so printing your fragment tells you nothing. To see the whole text, read element.text, which returns the visible label as a str.

About the assignment

You find two links by a single word each, then print the full label that the page shows for each one. The task tells you which word to use for each.

What you will do

  1. Search for the first link with the partial strategy, using the word from the task.
  2. Search for the second link the same way with its own word.
  3. Read the visible text of each element, not the fragment you searched for.
  4. Print them on two lines, with the labels Link 1: and Link 2:.

Common mistakes

  • Using a fragment in the wrong case

    A lower-case search for a word that is capitalised on the page raises NoSuchElementException. Match the letters exactly.

  • Choosing a fragment that is too broad

    A word shared by several links returns only the first one. Check by running find_elements and looking at the length of the list.

  • Printing the search text

    If you print the string you searched with, the output shows a fragment, not the full label. Read the text from the element.

In a real test suite

Partial link text suits navigation menus where the label begins with an icon or a counter, such as Inbox (3). A fragment of the stable part keeps the locator working as the number changes.

The next lesson stops asking for one element and asks for every element that fits a rule, returned as a list.

To try one of these calls on any page without a task, open the Online Selenium Python Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

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

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/Links")
    # Find links by partial text TextFields and Buttons
finally:
    driver.quit()