1. Working with Attributes
Attributes are the most common entry point for XPath locators. XPathy exposes all HTML attributes as objects, each with chainable methods.
Import all attributes:
import static com.xpathy.Attribute.*;
Contains on id
XPathy locator = id.contains("login-button");
//*[contains(@id, 'login-button')]
Equals on class
XPathy locator = class_.equals("active");
//*[@class='active']
StartsWith on data-testid
XPathy locator = data_testid.startsWith("menu-");
//*[starts-with(@data-testid, 'menu-')]
Numeric comparisons on value
XPathy locator = value.greaterThan(100);
// Result: //*[@value > 100]
XPathy locator = value.lessThan(50);
// Result: //*[@value < 50]
haveIt() (checks if attribute
exists), isEmpty() (confirms attribute is empty), and isNumeric()
(ensures value is numeric).
2. Attributes within Specific Tags
XPathy allows scoping attributes inside specific HTML tags, making locators more precise.
Import all tags:
import static com.xpathy.Tag.*;
Find a <div> by id
XPathy locator = div.byAttribute(id).equals("main-container");
//div[@id='main-container']
Find a <h2> by class
XPathy locator = h2.byAttribute(class_).equals("section-title");
//h2[@class='section-title']
Find a <p> by data-testid
XPathy locator = p.byAttribute(data_testid).startsWith("paragraph-");
//p[starts-with(@data-testid, 'paragraph-')]
equals, contains,
startsWith, greaterThan, etc.) works with every supported tag.
3. Working with Text Content
XPathy provides intuitive methods for targeting visible text inside elements.
Text contains
XPathy locator = div.byText().contains("Welcome");
//div[contains(text(), 'Welcome')]
Text starts with
XPathy locator = h2.byText().startsWith("Chapter");
//h2[starts-with(text(), 'Chapter')]
Global Text usage
XPathy locator = Text.contains("Error");
// Result: //*[contains(text(), 'Error')]
XPathy locator = Text.startsWith("Success");
// Result: //*[starts-with(text(), 'Success')]
This is useful when attributes are dynamic but the element text is stable.
4. Numeric Values Inside Elements
Some elements display numbers, such as counters or prices. XPathy lets you build conditions around them.
Greater than numeric content
XPathy locator = td.byNumber().greaterThan(10);
//td[number(text()) > 10]
Between numeric values
XPathy locator = span.byNumber().between(5, 15);
//span[number(text()) >= 5 and number(text()) <= 15]
This is especially handy for table cells or statistic widgets.
5. Working with Styles
Inline styles can be targeted when attributes or text are insufficient.
Check inline style within a tag
XPathy locator = div.byStyle(backgroundColor).equals("#000000");
//div[contains(translate(@style, ' ', ''), 'background-color:#000000;')]
Check inline style directly
import static com.xpathy.Style.*;
XPathy locator = backgroundColor.equals("#000000");
//*[contains(translate(@style, ' ', ''), 'background-color:#000000;')]