Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath to get Node containing text

Tags:

xml

xpath

I tried to search for nodes containing text 'Yahoo' under '/doc/story/content', it returns 'content' node, but I need exact text node that contains 'Yahoo' or it's parent

<doc>     <story>         <content id="201009281450332423">             <ul>MSW NYNES NYPG1 DILMA</ul>             <p> <k> Yahoo, made </k> it nice </p>             <p>                <author>-v-</author>             </p>         </content>     </story> </doc> 

Xpath: "/doc/story/content[contains(., 'Yahoo')]"

like image 552
Vjy Avatar asked Jun 22 '11 15:06

Vjy


People also ask

How find XPath text contains?

The syntax for locating elements through XPath- Using contains() method can be written as: //<HTML tag>[contains(@attribute_name,'attribute_value')]

Can we use text () in XPath?

5) XPath Text() FunctionThe XPath text() function is a built-in function of selenium webdriver which is used to locate elements based on text of a web element. It helps to find the exact text elements and it locates the elements within the set of text nodes. The elements to be located should be in string form.

How do I select text in XPath?

Do note that /html/text() doesn't select all text nodes in the document -- only the text nodes that are children (not descendents) of the top, html element. You probably want /html//text() . Some knowledge and understanding of XPath is typically required in order to construct XPath expressions.

How will you write XPath if the tag has only text?

We will start to write XPath with //, followed by input tag, then we will use the select attribute, followed by its attribute name like name, id, etc, and then we will choose a value of attribute in single quotes. Here, (1 of 1) means exact match. It indicates that there is only one element available for this XPath.


1 Answers

Since you need all textNodes only which contain the text Yahoo, use the following XPath.

//text()[contains(., 'Yahoo')] 

This should return you all the textNodes only which contains Yahoo (case-sensitive) in it.

like image 99
Ravish Avatar answered Sep 22 '22 07:09

Ravish