About this lesson
Selenium Python XPath contains() and starts-with(): match partial text or an id prefix
Selenium Python AssignmentsBeginnerCSS and XPath Locators
How it works
Real pages rarely give you the exact text or id you want to type. XPath has two string functions for that. contains(a, b) is true when the first value includes the second anywhere inside it. starts-with(a, b) is true only when the first value begins with the second. You put either one inside the square brackets of a path, and the first argument is usually an attribute such as @id or the text of the element.
Both functions are case sensitive, so copy the casing from the page. On this page three practice links share a common word, so find_element would return the first of them if your match were too loose. A narrower piece of text, or the prefix of an id, picks the one you want.
Watch the arguments. Forgetting the parentheses after text makes XPath look for a child element called text, which matches nothing. Passing one argument instead of two raises InvalidSelectorException. XPath expressions hold quotes of their own, so use different quote marks for the Python string and for the values inside it.
About the assignment
On the Links page, locate two practice links without using their full text or full id. The first is found through part of its visible text, the second through the beginning of its id. Print what each link shows.
What you will do
- Write an XPath for an
aelement that appliescontains()to its text, using the word from the task. - Write another XPath for an
aelement that appliesstarts-with()to itsid, using the prefix from the task. - Find both elements with
find_elementand read.textfrom each. - Print the two lines in order, with the labels from the task.
Common mistakes
Writing text without parentheses
A condition that names
textand omits the brackets looks for a child element with that name. The lookup raisesNoSuchElementException, and the message echoes your expression so you can spot the gap.Giving a function one argument
A call that holds only the search word is invalid XPath. Selenium raises
InvalidSelectorException: invalid selector, because both the value to search in and the value to search for are needed.Mixing up letter case
A capitalised prefix of this id finds nothing, and
find_elementraisesNoSuchElementException. Match the capitals exactly as they appear in the page source.
In a real test suite
Partial matching is a common answer to ids with a generated suffix, such as a fixed name followed by a changing number. A page object stores that XPath once, and the tests keep calling the same method.
The next lesson goes in a different direction: from an element you already hold, it climbs to the parent.
Related lessons
- Find Elements with XPathThe base XPath syntax with tags and attributes that this lesson extends.
- Match a Link with By.LINK_TEXTMatches a link by its full text with
By.LINK_TEXT, the exact version of what you do here. - Match a Link with By.PARTIAL_LINK_TEXTMatches a link by part of its text with
By.PARTIAL_LINK_TEXT, a shorter alternative tocontains().
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")
# Use contains and starts-with, then print both link texts
finally:
driver.quit()