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
//input

Select 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

  1. Use IDs: //button[@id='submit']

  2. Use data-testid: //div[@data-testid='menu']

  3. Normalize Text: [normalize-space()='Text']

  4. Combine Attributes: [@role='button' and @aria-label='Submit']

  5. Use Parent Anchors: //div[@id='form']//button

CSS Best Practices

  1. Use IDs: #submit

  2. Use Classes: .button-primary

  3. Combine Selectors: button.primary#submit

  4. Use Attributes: [name='email']

  5. Keep Simple: Avoid overly complex selectors


← Back to Documentation

Last updated