How It Works
Instead of chaining and(), or(), and not() inline, you
can use the Condition helper methods to group multiple
conditions explicitly. This helps when certain expressions need parentheses for precedence.
Import static methods:
import static com.xpathy.Condition.*;
Nested Login Validation
XPathy locator = div.byCondition(
and(
text().startsWith("Login"),
or(
text().contains("Button"),
attribute(id).contains("auth-btn")
),
not(attribute(class_).withCase(IGNORED).contains("disabled"))
)
);
//div[(starts-with(text(), 'Login')
and (contains(text(), 'Button') or contains(@id, 'auth-btn'))
and not(contains(translate(@class, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'), 'disabled')))]
Description:
- Must start with the word
Login - Must either contain the text
Buttonor have an ID containingauth-btn - Must not contain the class
disabled(case-insensitive)
Use Case: On a login form, the submit button may appear as
Login Button, Login, or even dynamically generated with ID
auth-btn-123. Sometimes the button is disabled with disabled
class. This nested locator ensures you only match the correct, enabled login button.
Product Label with Nested Rules
XPathy locator = span.byCondition(
or(
text().contains("Premium"),
and(
attribute(class_).equals("highlight"),
attribute(data_testid).contains("featured"),
not(text().contains("Expired"))
)
)
);
//span[
contains(text(), 'Premium')
or (
@class='highlight'
and contains(@data-testid, 'featured')
and not(contains(text(), 'Expired'))
)
]
Description:
- Matches
<span>elements that either contain the wordPremiumOR - Have class equal to
highlight, adata-testidcontainingfeatured, and text that does not containExpired
Use Case: In an e-commerce site, premium products may be labeled with the
word Premium in the text, or tagged structurally with a highlight
class and data-testid attribute like featured-product. Expired
promotions should be excluded.
Benefits of Nested Conditions
- Clarity: Explicit parentheses in code mirror how the XPath will evaluate
- Maintainability: Easy to add or remove conditions without breaking the structure
- Flexibility: Supports mixing attributes, text, styles, and transformations
- Accuracy: Guarantees correct precedence when combining multiple conditions