Home > Software engineering >  What does contains(.,'substring') mean in XPath and what's its equivalent in CSS?
What does contains(.,'substring') mean in XPath and what's its equivalent in CSS?

Time:08-24

Able to locate all div elements in UI page using this XPATH locator : //div[contains(.,'')] irrespective of the text inside them. What does .,'' mean ?

Consider below example:

XPath to specifically select div with text - 'Apple' would be //div[contains(.,'Apple')].

What is its CSS equivalent ?

<div>
<span> Apple </span>
</div>

CodePudding user response:

//div[contains(.,'Apple')]

  1. //div means to select all div elements in the document.
  2. //div[ predicate ] means to filter those per the given predicate.
  3. contains( str, substr ) means to return true iff str contains the substring, substr.
  4. . is the context node. (See Current node vs. Context node in XSLT/XPath?) Within the predicate of your XPath, it will be a div element. When passed as a function parameter with type string, it will be converted to the string value of the node.
  5. The string-value of an element is equal to the concatenation of the string values of its children elements.

Therefore, your XPath returns all div elements in the document whose string value contains the 'Apple' substring.

There is no CSS equivalent.

See also

  • Related