Home > OS >  XPath to text returning nothing?
XPath to text returning nothing?

Time:07-17

I'm trying to address the text Aug 7, 2019 at 9:34 am ET in the following code with XPath:

<span ><span >
<a href="https://example.com" title="Posts by me" rel="author">Author</a></span> | Aug 7, 2019 at 9:34 am ET
</span>

I use this Xpath expressions //span[@]/text() , which always worked for me in such cases - but fail and get just emptyness.

I tried some variants too, like //span[@]/text[0] and //span[@]/text[1] - but failed too.

What could be the correct Xpath to Aug 7, 2019 at 9:34 am ET?

CodePudding user response:

If //span[@]/text() is not returning what you want, then there may be a text node, perhaps containing only whitespace, before your targeted text. (That expression will return all text node children of the targeted span, but in XPath 1.0 as an argument to a function requiring a string, only the first node of a node set is used.)

Since you probably don't want the preceding | anyway, you might try calling substring-after on the string value of the parent element...

This XPath 1.0 expression,

substring-after(//span[span/a/@rel="author"],' |')

will evaluate to

Aug 7, 2019 at 9:34 am ET

as requested.

CodePudding user response:

Try:

//span[@]/../text[1]

OR

//span[@]/text()

After selecting the element locator stategy which is //span[@] then invoke another node that's text nodes /text() and get the desired text value

example

  • Related