selenium xpath 混合内容 html span 的抓取
我正在尝试抓取具有混合内容的跨度元素
<span id="span-id">
<!--starts with some whitespace-->
<b>bold title</b>
<br/>
text here that I want to grab....
</span>
,这是识别跨度的抓取代码片段。它可以毫无问题地拾取它,但网络元素的文本字段是空白的。
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://page-to-examine.com");
var query = driver.FindElement(By.XPath("//span[@id='span-id']"));
我尝试将 /text() 添加到表达式中,但它也不返回任何内容。如果我添加 /b 我确实得到了粗体文本的文本内容 - 这恰好是我不感兴趣的标题。
我确信使用一点 xpath 魔法这应该很容易,但我没有找到到目前为止!或者有更好的方法吗?如有任何意见,我们深表谢意。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将选择上下文节点的所有文本节点子节点 - 并且有以下三个他们。
您所指的“无”很可能是其中的第一个,它是一个仅包含空格的文本节点(因此您在其中看到“无”)。
您需要的是:
当然,还有其他可能的变化:
或者:
基于 XSLT 的验证:
此转换只是输出任何内容XPath 表达式选择。当应用于提供的 XML 文档时(注释已删除):
产生了想要的结果:
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:
我相信以下 xpath 查询应该适合您的情况。 follow-sibling 对于您想要做的事情很有用。
I believe the following xpath query should work for your case. following-sibling useful for what you're trying to do.