I'm trying to search DOM elements using DOM Node's properties, like innerText
and textContent
, as filters, but without success.
Take this HTML for instance:
<html>
<body>
<div class="one">
<div class="two">
<div class="three">
<div>Text Here</div>
</div>
</div>
</div>
</body>
</html>
If I search for a node using the class attribute I can access the innerText
property, both with XPath (evaluate) and CSS Selector (querySelectorAll) functions:
document.evaluate("//div[@class = 'one']",
document, null, XPathResult.FIRST_ORDERED_NODE_TYPE,
null).singleNodeValue.innerText;
document.querySelectorAll("div.one")[0].innerText;
However, using innerText
property instead of class
gives me no result:
document.evaluate("//div[@innerText = 'Text Here']",
document, null, XPathResult.FIRST_ORDERED_NODE_TYPE,
null).singleNodeValue;
document.querySelectorAll("div[innerText='Text Here']")[0];
I tested with the nodeName
and textContent
and it failed as well, so this is why I think it is related with this DOM properties which are not tag attributes.
I tried using XPath text()
function, but using it on <div class="one">
from the example above won't return "Text Here".
All the tests were made on Google Chrome v67, on Linux Mint.
Why does this happens? Is there any solution or workaround for what I'm trying to do?