Get (text) in XPath

I have the following DOM / HTML structure, I want to get (just by practicing ...) the marked data. enter image description here

One that is under the h2 element. that the div element [@ class = "coordsAgence"] has a few more div children below and a few more h2 .. so that:

div[@class="coordsAgence"] 

Will get this value, but with additional unnecessary text. UPDATE The value (from this example) that I basically want is that: the text is "GALLIER Dennis".

+6
dom html html-parsing xpath
source share
2 answers

It seems you need the first node text in this div:

 div[@class="coordsAgence"]/text()[1] 

must do it.

Note that this assumes that there are no spaces between these comments within <div class="coordsAgence"> ; otherwise, spaces will be additional text nodes that you will have to consider.

+11
source share

Get the first node text following the first h2 in the div with class "coordsAgence" :

 div[@class='coordsAgence']/h2[1]/following-sibling::text()[1] 

Note that this first expression returns the first text node after the first h2 , even if another node is displayed between them. If you want to return text only when it is the node that follows the first h2 , try something like this:

 div[@class='coordsAgence']/h2[1][following-sibling::node()[1][self::text()]]/following-sibling::text()[1] 
0
source share

All Articles