Home Documentation Logical Operations

C - Basic Logical Operations

Combine multiple conditions with and(), or(), and not() operators to create powerful compound locators.

1. and() Operator

The .and() operator joins two conditions that must both be true.

Example
XPathy locator = div.byAttribute(id).equals("main-container")
                    .and()
                    .byText().contains("Hello World");
Result
//div[@id='main-container' and contains(text(), 'Hello World')]

Explanation:

  1. div.byAttribute(id).equals("main-container")//div[@id='main-container']
  2. .and().byText().contains("Hello World") → adds an additional condition on the same div node
  3. Final → //div[@id='main-container' and contains(text(), 'Hello World')]

2. or() Operator

The .or() operator joins two conditions where either one may be true.

Example
XPathy locator = div.byAttribute(id).equals("main-container")
                    .or()
                    .byText().contains("Hello World");
Result
//div[@id='main-container' or contains(text(), 'Hello World')]

Explanation: Matches any <div> with id="main-container" OR text containing "Hello World".

3. not() Operator

The .not() operator negates the following condition. This allows you to exclude elements matching a certain attribute, text, or style.

Example
XPathy locator = div.byText().contains("Hello World")
                    .and()
                    .byAttribute(id).not().equals("main-container");
Result
//div[contains(text(), 'Hello World') and not(@id='main-container')]

Explanation:

  1. First condition: contains(text(), 'Hello World')
  2. Second condition: not(@id='main-container')
  3. Combined with .and()//div[contains(text(), 'Hello World') and not(@id='main-container')]

4. Chaining Multiple Logical Operations

XPathy allows chaining and(), or(), and not() in sequence to build more complex predicates.

Example
XPathy locator = span.byText().contains("Discount")
                     .and()
                     .byAttribute(class_).not().equals("expired")
                     .or()
                     .byNumber().greaterThan(50);
Result
//span[contains(text(), 'Discount') and not(@class='expired') or number(text()) > 50]
Usage Tips:
  • Parentheses are automatically handled to preserve correct evaluation order
  • You can mix attribute, text, number, and style conditions freely
  • Use .not() immediately before .equals(), .contains(), .startsWith(), etc.
Summary: With logical operators, XPathy expressions scale from simple attribute checks to full-fledged business rules written in a clear, fluent style.