I need to extract the text from the nodes in an html file and I'm trying to use XPath and Javascript.
The required condition is that the text must contain an specific word.
Let's take by example the next html file:
<html>
<body>
<p>
Hi, try to extract the word username here and here <b>username</b>
</p>
</body>
</html>
And try to get the text from text nodes containing the word 'username' with this expression:
var search = document.evaluate('//*[contains(child::text(), \"username\")]/child::text()', document, null, XPathResult.ANY_TYPE, null);
Iterating through search I've found the desired result but unwanted objects too:
["Hi, try to extract the word username here and here", Text, "username"]
where Text is an Object whose textContent is only the carriage return symbol (I'm using Google Chrome console). Where does this object come from?
Can anyone, please, give a more precise XPath expression that excludes those Objects or should I exclude them in my code?
The ideal search should be:
["Hi, try to extract the word username here and here", "username"]
Thanks everybody!
So, inorder to find the Text all you need to do is: driver. findElement(By. xpath("//*[contains(text(),'the text you are searching for')]"));
Locating Strategies- (By XPath- Using text()) In this section, you will learn how to locate a particular web element by XPath- Using text() method. "text() method" is used to identify an element based on the text available on the web page.
XPath text() function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes. The elements that need to be found should be in string format.
Texts are nodes. Some elements contain other nodes. Some elements contain text nodes.
Looks like you want
var search = document.evaluate('//text()[contains(., \"username\")]',
document, null, XPathResult.ANY_TYPE, null);
(I'm not sure why you're escaping your double quotes inside single quotes, but that's a separate issue.)
Your existing code,
var search = document.evaluate('//*[contains(child::text(), \"username\")]/
child::text()', document, null, XPathResult.ANY_TYPE, null);
says,
The (1) part of the expression will return element nodes <p>
and <b>
.
For the (2) step, <b>
has only one text node child, but <p>
has two: the one before the <b>
(which contains "username") and the one after the <b>
(which contains only whitespace).
Solution: Forget about elements -- they are an irrelevant distraction. Just select the desired text nodes directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With