Selenium XPath And Operator

Profile picture for user arilio666

The AND operator in XPath combines two boolean expressions and returns true. This is only possible if only both expressions are true.

In XPath, AND can fetch elements that match multiple conditions. AND operator, when applied to dual attributes, identifies elements when all are pointing to that specific element.

Syntax:

//div[@class='name' and @type='name']

Consider this below DOM.

<a id="1">
  <b>value1</b>
  <b>value2</b>
</a>

Consider the scenario where we want to get the id of <a>, and there are two <b> nodes with value1 and value2.

//a[b = 'value1' and b = 'value2']/@id

This will select the id attributes of all a elements with child b elements having the value1 and value2 text.