我正在尝试刮掉混合内容的span元素
<span id="span-id">
<!--starts with some whitespace-->
<b>bold title</b>
text here that I want to grab....
</span>
这是一个标识 Span 的抓取代码片段 . 它没有问题就接了它,但是webelement的文本字段是空白的 .
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://page-to-examine.com");
var query = driver.FindElement(By.XPath("//span[@id='span-id']"));
我已经尝试将/ text()添加到表达式中,该表达式也不返回任何内容 . 如果我添加/ b我会得到粗体文本的文本内容 - 这恰好是我不感兴趣的 Headers .
我肯定有一点xpath魔法,这应该很容易,但到目前为止我还没找到它!或者,还有更好的方法?感激地收到任何评论 .
2 回答
这将选择上下文节点的所有text-node-children - 并且有三个 .
你所谓的“无”是最可能的第一个,这是一个只有空格的文本节点(因此你看到它中没有“) .
What you need is :
Of course, there are other variations possible :
要么:
XSLT-based verification :
This transformation simply outputs whatever the XPath expression selects. When applied on the provided XML document (已删除评论):
the wanted result is produced :
我相信以下xpath查询应该适用于您的情况 . 跟随兄弟姐妹对你正在尝试做的事情有用 .