Skip to main content

XPath queries

XPath-like query engine for visual and logical trees.

using Avalonia.HeadlessTestingFramework;

var xpath = new TreeXPath(window);

// Select by type
var buttons = xpath.Select<Button>("//Button");

// Select by path
var nestedButton = xpath.SelectFirst<Button>("/Window/Grid/StackPanel/Button");

// Select with attributes
var submitBtn = xpath.SelectFirst<Button>("//Button[@Name='submit']");

// Select by index
var secondButton = xpath.SelectFirst<Button>("//StackPanel/Button[2]");

// Select descendants
var allTextBlocks = xpath.Select<TextBlock>("//TextBlock");

// Complex queries
var enabledPrimaryBtns = xpath.Select<Button>(
"//Button[@IsEnabled='true' and contains(@Classes, 'primary')]");

// Check existence
bool hasErrors = xpath.Exists("//TextBlock[@Classes='error']");

// Count matches
int buttonCount = xpath.Count("//Button");

// Select with predicates
var largeButtons = xpath.Select<Button>("//Button[Width > 100]");

// Parent axis
var buttonParent = xpath.SelectFirst("//Button[@Name='submit']/..");

// Sibling axis
var nextSibling = xpath.SelectFirst("//Button[@Name='first']/following-sibling::Button");

Supported XPath Syntax​

ExpressionDescription
//TypeSelect all descendants of type
/Path/To/TypeSelect by absolute path
Type[@Name='value']Filter by property
Type[index]Select by position (1-based)
Type[@Prop='val' and @Prop2='val2']Multiple conditions
Type[contains(@Prop, 'text')]Contains function
Type[starts-with(@Name, 'btn')]Starts-with function
..Parent node
following-sibling::TypeFollowing siblings
preceding-sibling::TypePreceding siblings
ancestor::TypeAncestor nodes
descendant::TypeDescendant nodes