Selenium xpath scrape mixed content html span

I am trying to clear a span element that has mixed content

<span id="span-id"> <!--starts with some whitespace--> <b>bold title</b> <br/> text here that I want to grab.... </span> 

And here is a snippet of capture code that identifies the range. He selects it without any problems, but the text box of the web element is empty.

 IWebDriver driver = new FirefoxDriver(); driver.Navigate().GoToUrl("http://page-to-examine.com"); var query = driver.FindElement(By.XPath("//span[@id='span-id']")); 

I tried adding / text () to an expression that also returns nothing. If I add / b, I get the text content in bold - this is probably the name that interests me not.

I'm sure it should be easy with the xpath mask, but I still can't find it! Or is there a better way? Any comments gratefully received.

+7
source share
2 answers

I tried adding /text() to an expression that also returns nothing

This selects all text node node context detectors β€” and there are three of them.

What you call "nothing" is most likely the first of them, which is the text of only the white space node (so you see "nothing" in it).

What do you need :

 //span[@id='span-id']/text()[3] 

Of course, other options are possible :

 //span[@id='span-id']/text()[last()] 

Or:

 //span[@id='span-id']/br/following-sibling::text()[1] 

XSLT Based Validation :

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="node()|@*"> "<xsl:copy-of select="//span[@id='span-id']/text()[3]"/>" </xsl:template> </xsl:stylesheet> 

This conversion simply displays all the expressions selected by XPath. When applied to the provided XML document (comment deleted):

 <span id="span-id"> <b>bold title</b> <br/> text here that I want to grab.... </span> 

The desired result is obtained :

  " text here that I want to grab.... " 
+4
source

I believe the following xpath request should work for your case. the next is a brother useful for what you are trying to do.

 //span[@id='span-id']/br/following-sibling::text() 
+3
source

All Articles