我正在尝试根据里面的文字找到一个元素。例如,要找到此链接:
<a href="#">Foobar</a>
使用下面的代码,null
在console.log(matchingElement)
两种情况下都会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>
<a target="_blank" href="{% url "admin:auth_user_changelist" %}">Utilisateurs</a>