Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath: Is there a way to get all the children's text in xpath

Tags:

xpath

I'm using the xpath engine on Firefox. I have html:

<span>
   <b>prefix one</b> not bold part
</span>
<span>
   prefix two not bold part
</span>

I want all the spans that have child text that start with "prefix one".

I tried the xpath:

//span[starts-with(child::text(), "prefix one")]

but this doesn't work because the b tag is interfering. How do you solve this?

thanks

like image 861
Guy Avatar asked Jan 04 '10 14:01

Guy


People also ask

How do I select the first child in XPath?

The key part of this XPath is *[1] , which will select the node value of the first child of Department .

What is text () 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.


1 Answers

If you know that spans is not nested into other spans you can try this:

//span[ starts-with( descendant-or-self::*/text(),"prefix one" ) ]

descendant-or-self::*/text(), should return all text nodes which are in this subtree. I don't know how starts-with() exactly works but I suppose when some of text() nodes in subtree starts with "prefix one" that condition is true

like image 172
Gaim Avatar answered Dec 21 '22 00:12

Gaim