Skip to content
CodeKruPro

XPath Functions - contains() and starts-with()

14/70

XPath Functions

XPath provides functions for flexible matching:

  • contains(@attr, 'value') - attribute contains text
  • starts-with(@attr, 'value') - attribute starts with text
  • contains(text(), 'value') - element text contains text

Relevant HTML structure:

<a id="seleniumLink" href="...">Practice TextFields</a>
<a id="playwrightLink" href="...">Practice Buttons</a>
<a id="cypressLink" href="...">Practice Checkboxes</a>

Task:

  1. Navigate to https://testkru.com/Elements/Links
  2. Find link where text contains "TextFields" using XPath
  3. Print its full text
  4. Find link where ID starts-with "cypress" using XPath
  5. Print its full text

Expected output:

Contains 'TextFields': Practice TextFields
Starts with 'cypress': Practice Checkboxes
Read full tutorial on Codekru

About this lesson

XPath contains() and starts-with() in Selenium Java: match part of the text or an attribute

Learn Selenium JavaBeginnerCSS and XPath Locators

How it works

contains(a, b) is true when string a includes string b anywhere, and starts-with(a, b) when a begins with it. The first argument says what to test: @id or @href for an attribute, text() for the element's own text, or . for all the text inside it, including text in child elements.

Browsers run XPath 1.0, which has no ends-with(). On Chrome it throws InvalidSelectorException. If you need that check, use the CSS $= attribute operator from lesson 12.

Both functions are case-sensitive and match plain substrings, so they can match more than you meant. On the Links page, //a[contains(@href,'Buttons')] matches three links, and the first is the sidebar link to Radio Buttons, which sits off-screen in the default headless window.

About the assignment

The Links page has three "Practice ..." links, each with its own id. You will find one by a word inside its visible text and another by the first letters of its id, then print each link's complete text.

What you will do

  1. Write an XPath for a elements whose text() contains the word given in the task, and pass it to By.xpath().
  2. Print the whole link text with getText() after the label used in the expected output.
  3. Write a second XPath that uses starts-with() on the id attribute with the prefix from the task.
  4. Print that link's text with its own label. Both labels quote the search word in single quotes, exactly as the expected output shows.

Common mistakes

  • Swapping the arguments

    contains('TextFields', text()) asks whether the fixed word contains the element's text. That is a different question, and nothing useful matches.

  • Lowercasing the search word

    XPath has no case-insensitive mode. //a[contains(text(),'textfields')] finds 0 links, so findElement() throws NoSuchElementException.

  • Using text() when the text is in a child

    text() only sees text that sits directly in the element. If the link wraps its label in a span, test . instead.

  • Trying to filter on text with CSS

    jQuery-style a:contains(...) is not real CSS. Chrome rejects it and Selenium throws InvalidSelectorException.

In a real test suite

Partial matching is how tests deal with ids such as submit-btn-8f3a, where the suffix changes on every build, and with link or button labels that carry a count, such as "Cart (3)".

Lesson 13 introduced the plain predicates. Next, lesson 15 moves through the tree itself with axes: from a child to its parent, and from a label to the element beside it.

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

Starter code

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

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/Links");
        
        // TODO: Find link where text contains "TextFields"
        // TODO: Find link where id starts with "cypress"
        
        driver.quit();
    }
}
Read the full tutorial