Home Documentation Basic Operations

A - Basic Operations

Learn the fundamental operations of XPathy including attributes, tags, text content, numeric comparisons, and style handling.

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

Example
XPathy locator = id.contains("login-button");
Result
//*[contains(@id, 'login-button')]

Equals on class

Example
XPathy locator = class_.equals("active");
Result
//*[@class='active']

StartsWith on data-testid

Example
XPathy locator = data_testid.startsWith("menu-");
Result
//*[starts-with(@data-testid, 'menu-')]

Numeric comparisons on value

Example
XPathy locator = value.greaterThan(100);
// Result: //*[@value > 100]

XPathy locator = value.lessThan(50);
// Result: //*[@value < 50]
Note: Additional methods include 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

Example
XPathy locator = div.byAttribute(id).equals("main-container");
Result
//div[@id='main-container']

Find a <h2> by class

Example
XPathy locator = h2.byAttribute(class_).equals("section-title");
Result
//h2[@class='section-title']

Find a <p> by data-testid

Example
XPathy locator = p.byAttribute(data_testid).startsWith("paragraph-");
Result
//p[starts-with(@data-testid, 'paragraph-')]
Tip: Every attribute method (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

Example
XPathy locator = div.byText().contains("Welcome");
Result
//div[contains(text(), 'Welcome')]

Text starts with

Example
XPathy locator = h2.byText().startsWith("Chapter");
Result
//h2[starts-with(text(), 'Chapter')]

Global Text usage

Example
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

Example
XPathy locator = td.byNumber().greaterThan(10);
Result
//td[number(text()) > 10]

Between numeric values

Example
XPathy locator = span.byNumber().between(5, 15);
Result
//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

Example
XPathy locator = div.byStyle(backgroundColor).equals("#000000");
Result
//div[contains(translate(@style, ' ', ''), 'background-color:#000000;')]

Check inline style directly

Example
import static com.xpathy.Style.*;

XPathy locator = backgroundColor.equals("#000000");
Result
//*[contains(translate(@style, ' ', ''), 'background-color:#000000;')]