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.
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:
Of course, there are other variations possible:
Or:
XSLT-based verification:
This transformation simply outputs whatever the XPath expression selects. When applied on the provided XML document (comment removed):
the wanted result is produced: