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 elements of the 'label' tag
// whose text exactly matches 'I have tried TestCafe'.
// 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.
// This selector matches the parent div (.container)
// and then the child div (.child)
const elWithChild = Selector('div').withText('foo');
Target elements with special characters
To target elements with HTML symbols and entities (for example, , newline characters), refer to the Select Elements That Contain Special Characters recipe.