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
- Search for the first link with the partial strategy, using the word from the task.
- Search for the second link the same way with its own word.
- Read the visible text of each element, not the fragment you searched for.
- Print them on two lines, with the labels
Link 1:andLink 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_elementsand 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.
Related lessons
- Match a Link with By.LINK_TEXTRequires the entire label, which makes it the stricter sibling of this locator.
- Find Every Radio ButtonReturns a list of every match, which also tells you how many links there are.
- Fix a Locator That Does Not ExistStarts from a locator that finds nothing and fixes it.
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.
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()