Selector Types
Complete reference for XPath and CSS selector syntax in TrueAssert.
Overview
TrueAssert supports two selector types:
XPath: More expressive, recommended for complex pages
CSS: Simpler syntax, familiar to web developers
XPath Selectors
Basic Syntax
Select All Elements of Type:
//button
//div
//inputSelect by ID:
//*[@id='submit']
//button[@id='submit']Select by Attribute:
Select by Class:
Attribute Matching
Exact Match:
Contains:
Starts With:
Multiple Conditions (AND):
Multiple Conditions (OR):
Text Matching
Exact Text:
Contains Text:
Normalize Space:
Removes leading/trailing whitespace and normalizes internal spaces.
Positional Selection
First Element:
Last Element:
Nth Element:
Position Range:
Path Navigation
Descendant (anywhere below):
Child (direct child):
Parent:
Following Sibling:
Preceding Sibling:
Advanced XPath
Multiple Attributes:
Nested Conditions:
Combining Paths:
Functions:
SVG Elements
Local Name:
Namespace Handling:
CSS Selectors
Basic Syntax
Element Type:
ID Selector:
Class Selector:
Attribute Selector:
Attribute Matching
Exact Match:
Contains:
Starts With:
Ends With:
Word Match:
Matches if value is a space-separated list and one word matches.
Combinators
Descendant (space):
Child (>):
Adjacent Sibling (+):
General Sibling (~):
Pseudo-classes
First Child:
Last Child:
Nth Child:
Not:
Empty:
Multiple Selectors
Grouping:
Combining:
When to Use Which
Use XPath When
✅ Complex DOM structures
✅ Need text matching
✅ Need positional selection
✅ SVG elements
✅ More expressive queries needed
✅ Cross-browser consistency important
Use CSS When
✅ Simple, straightforward selectors
✅ Familiar CSS syntax preferred
✅ Performance is critical (slightly faster)
✅ Simple attribute matching
Examples Comparison
Same Selector in Both
XPath:
CSS:
XPath:
CSS:
XPath:
CSS:
(Note: CSS can't match by text content easily)
Best Practices
XPath Best Practices
Use IDs:
//button[@id='submit']Use data-testid:
//div[@data-testid='menu']Normalize Text:
[normalize-space()='Text']Combine Attributes:
[@role='button' and @aria-label='Submit']Use Parent Anchors:
//div[@id='form']//button
CSS Best Practices
Use IDs:
#submitUse Classes:
.button-primaryCombine Selectors:
button.primary#submitUse Attributes:
[name='email']Keep Simple: Avoid overly complex selectors
Related Topics
Understanding XPath Selectors - How XPath works
Selector Best Practices - Create good selectors
Command Reference - Commands that use selectors
Last updated