Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find text nodes that contain a word with xpath in dom

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!

like image 699
dysfuntcional Avatar asked Oct 08 '12 13:10

dysfuntcional


People also ask

How do I find a specific text in XPath?

So, inorder to find the Text all you need to do is: driver. findElement(By. xpath("//*[contains(text(),'the text you are searching for')]"));

Can we use text () in XPath?

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.

What is text () function in XPath?

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.

Is text a node in Dom?

Texts are nodes. Some elements contain other nodes. Some elements contain text nodes.


1 Answers

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,

  • (1) Find all elements that have text node children that contain "username".
  • (2) Now return all the text node children of those elements (regardless of what they contain).

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.

like image 173
LarsH Avatar answered Oct 22 '22 20:10

LarsH