1

I'm trying to find an element based on the text inside. For example, to find this link:

<a href="#">Foobar</a>

Using the codes below I get null on console.log(matchingElement) in both cases

var xpath = "a[text()='Foobar']";
var matchingElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
console.log(matchingElement);


xpath = "a[contains(text(),'Foobar')]";
matchingElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
console.log(matchingElement);
<a href="#">Foobar</a>
flx
  • 1,560
  • 13
  • 22
user1077220
  • 1,483
  • 5
  • 17
  • 21

0 Answers0