我想选择所有包含文本的节点。
在此示例中,不应选择外部 shouldBeIgnored
标记:
<shouldBeIgnored>
<span>
the outer Span should be selected
</span>
</shouldBeIgnored>
其他一些帖子建议这样://*/text()
。
但是,这在 firefox 中不起作用。
这是重现问题的小型单元测试:
public class XpathTest {
final WebDriver webDriver = new FirefoxDriver();
@Test
public void shouldNotSelectIgnoredTag() {
this.webDriver.get("http://www.s2server.de/stackoverflow/11773593.html");
System.out.println(this.webDriver.getPageSource());
final List<WebElement> elements = this.webDriver.findElements(By.xpath("//*/text()"));
for (final WebElement webElement : elements) {
assertEquals("span", webElement.getTagName());
}
}
@After
public void tearDown() {
this.webDriver.quit();
}
}
最佳答案
如果你想选择所有包含文本的节点,那么你可以使用
//*[text()]
上面的 xpath 将查找任何包含文本的元素。注意 text()
判断当前节点是否有文本的函数。
在您的情况下,它将选择 <span>
标签,因为它包含文本。
关于firefox - 使用 Firefox WebDriver 时选择所有具有文本的节点的 Xpath 表达式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11773593/