I'm trying to scrape 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's a code snippet of a grab that identifies the span. It picks it up without a problem but the text field of the webelement is blank.
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://page-to-examine.com");
var query = driver.FindElement(By.XPath("//span[@id='span-id']"));
I've tried adding /text() to the expression which also returns nothing. If I add /b I do get the text content of the bolded text - which happens to be a title that I'm not interested in.
I'm sure with a bit of xpath magic this should be easy but I'm not finding it so far!! Or is there a better way? Any comments gratefully received.
I've tried adding
/text()to the expression which also returns nothing
This selects all the text-node-children of the context node -- and there are three of them.
What you refer to "nothing" is most probably the first of these, which is a white-space-only text node (thus you see "nothing" in it).
What you need is:
//span[@id='span-id']/text()[3] 
Of course, there are other variations possible:
//span[@id='span-id']/text()[last()] 
Or:
//span[@id='span-id']/br/following-sibling::text()[1] 
XSLT-based verification:
<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 transformation simply outputs whatever the XPath expression selects. When applied on the provided XML document (comment removed):
<span id="span-id">
    <b>bold title</b>
    <br/>
    text here that I want to grab....   
</span>
the wanted result is produced:
     "
    text here that I want to grab....   
"
                        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