XPathy provides intuitive methods for navigating the DOM tree. All navigation methods start with
$ for easy identification.
1. $tag(tag)
Targets a nested tag under the current element.
Example
Result
XPathy locator = div.byAttribute(class_).equals("container")
.$tag(button)
.byText().equals("Submit");
//div[@class='container']//button[text()='Submit']
2. $child()
Restricts traversal to immediate child elements.
Any Child
Result
XPathy locator = ul.byAttribute(id).equals("menu")
.$child()
.byText().contains("Home");
//ul[@id='menu']/child::*[contains(text(), 'Home')]
Specific Child Tag
Result
XPathy locator = ul.byAttribute(id).equals("menu")
.$child(li)
.byText().contains("Contact");
//ul[@id='menu']/child::li[contains(text(), 'Contact')]
3. $ancestor()
Moves upward in the DOM to match ancestor elements.
Any Ancestor
Result
XPathy locator = a.byAttribute(href).contains("profile")
.$ancestor()
.byAttribute(id).equals("navbar");
//a[contains(@href, 'profile')]/ancestor::*[@id='navbar']
Specific Ancestor Tag
Result
XPathy locator = span.byText().equals("Settings")
.$ancestor(div)
.byAttribute(class_).equals("dropdown");
//span[text()='Settings']/ancestor::div[@class='dropdown']
4. $descendant()
Matches descendant elements nested anywhere below the current node.
Specific Descendant
Result
XPathy locator = section.byAttribute(id).equals("content")
.$descendant(p)
.byText().contains("Welcome");
//section[@id='content']/descendant::p[contains(text(), 'Welcome')]
Any Descendant
Result
XPathy locator = div.byAttribute(class_).equals("card")
.$descendant()
.byAttribute(class_).equals("price");
//div[@class='card']/descendant::*[@class='price']
5. $parent() and $up()
Navigate one or more levels up the DOM.
Parent
Result
XPathy locator = span.byText().equals("$19.99")
.$parent(div)
.byAttribute(class_).equals("product");
//span[text()='$19.99']/parent::div[@class='product']
Move Up Multiple Levels
Result
XPathy locator = input.byAttribute(name).equals("email")
.$up(2)
.byAttribute(id).equals("form-container");
//input[@name='email']/../..[@id='form-container']
6. Sibling Navigation
$followingSibling()
Matches siblings that appear after the current element.
Example
Result
XPathy locator = label.byText().equals("Username")
.$followingSibling(input)
.byAttribute(type).equals("text");
//label[text()='Username']/following-sibling::input[@type='text']
$precedingSibling()
Matches siblings that appear before the current element.
Example
Result
XPathy locator = li.byText().equals("Contact")
.$precedingSibling()
.byText().equals("About");
//li[text()='Contact']/preceding-sibling::*[text()='About']