Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct XPath query to fetch div inner text

Tags:

html

dom

xpath

I am using XPath to query into HTML document where there is following div has text:

<div class="h1">2008 Honda Accord Coupe<small> — Wuse II</small></div>

I wanted to fetch only the inner text of the <div class="h1">2008 Honda Accord Coupe and not include the <small>inner text</small>

I am making following XPath query: //div[@class='h1'] which is definitely returning entire <div>...</div> node.

How can I only fetch portion without <small>...</small>

thanks

like image 786
Wikki Avatar asked Jun 16 '13 22:06

Wikki


People also ask

How to use inner text in XPath?

The XPath expression will return the reference of html element whose text contains the data entry. Innertext is used to select the node of innertext using XPath. The expression will specify the pattern which selects the xml nodes. XPath innertext is very useful and important.

What does text () do in XPath?

The 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.


1 Answers

Use the text() function:

//div[@class='h1']/text()

Tested in phpFiddle

like image 97
SomeShinyObject Avatar answered Oct 06 '22 23:10

SomeShinyObject