Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using XPath: how to exclude text in nested elements

if I have some html like the following

<div class=unique_id>    
  <h1 class="parseasinTitle">
    <span> Game Title </span>
 </h1>
 Game Developer
</div>

Is there a way I can use xpath to get JUST the "Game Developer" part of the text? From searching around I tried:

//div[@class='unique_id' and not(self::h1/span)]

But that still gives me the entire text "Game Title Game Developer".

like image 223
theFakeGramita Avatar asked Aug 13 '13 20:08

theFakeGramita


People also ask

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.

How do you find the nth element using XPath?

In case we need to identify nth element we can achieve this by the ways listed below. position() method in xpath. Suppose we have two edit boxes in a page with similar xpath and we want to identify the first element, then we need to add the position()=1.

Can we use XML in XPath?

XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system. XPath expressions can be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.


1 Answers

div[@class = 'unique_id']/text()[not(normalize-space() = '')]

or

div[@class = 'unique_id']/text()[last()]

depending on context.

Note that you still have to trim the resulting text node.

like image 131
Tomalak Avatar answered Oct 09 '22 14:10

Tomalak