Skip to content
CodeKruPro

Use contains and starts-with in XPath

16/80

Use contains and starts-with in XPath

contains matches part of the text. starts-with matches the beginning of an attribute. On this page the link ids are seleniumLink and cypressLink.

Task:

  1. Find the link whose text contains TextFields
  2. Find the link whose id starts with cypress
  3. Print both link texts

Print the lines below exactly.

Expected output:

Contains 'TextFields': Practice TextFields
Starts with 'cypress': Practice Checkboxes

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

  1. Write an XPath for an a element that applies contains() to its text, using the word from the task.
  2. Write another XPath for an a element that applies starts-with() to its id, using the prefix from the task.
  3. Find both elements with find_element and read .text from each.
  4. Print the two lines in order, with the labels from the task.

Common mistakes

  • Writing text without parentheses

    A condition that names text and omits the brackets looks for a child element with that name. The lookup raises NoSuchElementException, 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_element raises NoSuchElementException. 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.

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")
    # Use contains and starts-with, then print both link texts
finally:
    driver.quit()