Home Documentation Nested Conditions

G - Nested Logical Conditions

Build deeply structured expressions with nested combinations of and(), or(), and not() to represent complex business rules.

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

Example
XPathy locator = div.byCondition(
                and(
                        text().startsWith("Login"),

                        or(
                                text().contains("Button"),
                                attribute(id).contains("auth-btn")
                        ),

                        not(attribute(class_).withCase(IGNORED).contains("disabled"))
                )
        );
Result
//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 Button or have an ID containing auth-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

Example
XPathy locator = span.byCondition(
                or(
                        text().contains("Premium"),

                        and(
                                attribute(class_).equals("highlight"),
                                attribute(data_testid).contains("featured"),
                                not(text().contains("Expired"))
                        )
                )
        );
Result
//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 word Premium OR
  • Have class equal to highlight, a data-testid containing featured, and text that does not contain Expired

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
Summary: With nested logical conditions, XPathy can model real-world business rules like enabled login buttons or valid product labels in a fluent, readable style while generating precise XPath expressions.