1. and() Operator
The .and() operator joins two conditions that must both be
true.
XPathy locator = div.byAttribute(id).equals("main-container")
.and()
.byText().contains("Hello World");
//div[@id='main-container' and contains(text(), 'Hello World')]
Explanation:
div.byAttribute(id).equals("main-container")→//div[@id='main-container'].and().byText().contains("Hello World")→ adds an additional condition on the samedivnode- 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.
XPathy locator = div.byAttribute(id).equals("main-container")
.or()
.byText().contains("Hello World");
//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.
XPathy locator = div.byText().contains("Hello World")
.and()
.byAttribute(id).not().equals("main-container");
//div[contains(text(), 'Hello World') and not(@id='main-container')]
Explanation:
- First condition:
contains(text(), 'Hello World') - Second condition:
not(@id='main-container') - 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.
XPathy locator = span.byText().contains("Discount")
.and()
.byAttribute(class_).not().equals("expired")
.or()
.byNumber().greaterThan(50);
//span[contains(text(), 'Discount') and not(@class='expired') or number(text()) > 50]
- 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.