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".
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With