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
- Write an XPath for
aelements whosetext()contains the word given in the task, and pass it toBy.xpath(). - Print the whole link text with
getText()after the label used in the expected output. - Write a second XPath that uses
starts-with()on theidattribute with the prefix from the task. - 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, sofindElement()throwsNoSuchElementException.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 aspan, test.instead.Trying to filter on text with CSS
jQuery-style
a:contains(...)is not real CSS. Chrome rejects it and Selenium throwsInvalidSelectorException.
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.
Related lessons
- Find Element by Partial Link Text
By.partialLinkText()is the shortcut for the text case, but it only works on links. - CSS Selectors - Attribute SelectorsThe CSS operators
^=,$=and*=cover the attribute cases, including ends-with. - XPath Axes - Parent and SiblingMove from an element you matched to its parent or siblings with XPath axes.
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.
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();
}
}