Selector.withExactText Method

Finds elements with a textContent value identical to the specified string. The text argument is case-sensitive.

Selector().withExactText(text) → Selector
Argument Type Description
text String The element’s text content. The text argument is case-sensitive.

To locate elements that contain a specific string, use the withText method.

Example

// Selects 'label' elements with the 'I have tried TestCafe' text.
// Does not match 'bar', 'foobar', 'Foo'.
const elWithText = Selector('label').withExactText('I have tried TestCafe');

Query results and the DOM Tree

The withText and withExactText methods match all the elements that contain the query string. It doesn’t matter whether the string descends directly from the element, or from one of its children. As long as TestCafe finds the string somewhere inside the target element, it’s a successful match.

Consider this piece of markup:

<div class="container">
    <div class="child">foo</div>
</div>

The following query matches both the .container class div and the .child class div.

// Selects label elements that contain 'foo' (case-insensitive).
// Matches 'foo', 'FOO', 'Foo', 'FooBar'.
const elWithCaseInsFoo = Selector('label').withText(/foo/i);

Target elements with special characters

To target elements with HTML symbols and entities (for example, &nbsp;, newline characters), refer to the Select Elements That Contain Special Characters recipe.