SGB*_*SGB 7 html selenium xpath selenium-webdriver
我正在尝试刮掉混合内容的span元素
<span id="span-id">
<!--starts with some whitespace-->
<b>bold title</b>
<br/>
text here that I want to grab....
</span>
Run Code Online (Sandbox Code Playgroud)
这是一个标识跨度的抓取代码片段.它没有问题就接了它,但是webelement的文本字段是空白的.
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://page-to-examine.com");
var query = driver.FindElement(By.XPath("//span[@id='span-id']"));
Run Code Online (Sandbox Code Playgroud)
我已经尝试将/ text()添加到表达式中,该表达式也不返回任何内容.如果我添加/ b我会得到粗体文本的文本内容 - 这恰好是我不感兴趣的标题.
我肯定有一点xpath魔法,这应该很容易,但到目前为止我还没找到它!或者,还有更好的方法?感激地收到任何评论.
我尝试添加
/text()到表达式中,它也不会返回任何内容
这将选择上下文节点的所有文本节点子节点——其中有三个。
您所指的“无”很可能是其中的第一个,它是一个仅包含空格的文本节点(因此您在其中看到“无”)。
你需要的是:
//span[@id='span-id']/text()[3]
Run Code Online (Sandbox Code Playgroud)
当然,还有其他可能的变化:
//span[@id='span-id']/text()[last()]
Run Code Online (Sandbox Code Playgroud)
或者:
//span[@id='span-id']/br/following-sibling::text()[1]
Run Code Online (Sandbox Code Playgroud)
基于 XSLT 的验证:
<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>
Run Code Online (Sandbox Code Playgroud)
此转换仅输出 XPath 表达式选择的任何内容。当应用于提供的 XML 文档时(注释已删除):
<span id="span-id">
<b>bold title</b>
<br/>
text here that I want to grab....
</span>
Run Code Online (Sandbox Code Playgroud)
产生了想要的结果:
"
text here that I want to grab....
"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3185 次 |
| 最近记录: |