如何跳过父节点(Xpath)中的子节点

时间:2017-05-27 06:57:10

标签: selenium xpath

<div id="blurb">
   Software Quality Assurance &amp; Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. Join them; it only takes a minute:
   <br>
   <br>
   <a href="/users/signup?ssrc=hero&amp;returnurl=https%3a%2f%2fsqa.stackexchange.com%2f" id="tell-me-more" class="button">Join</a>
</div>

我有以上HTML标记。我需要从父标记 div 节点中提取以下文本,不包括其子节点:

  

软件质量保证&amp;测试堆栈交换是软件质量控制专家,自动化工程师和软件测试人员的问答网站。加入他们;它只需要一分钟:

但是,我正在使用的xpath也从子节点Join中提取<a>

以下是我尝试的xpath

//div[@id='blurb']/*[not(@id='tell-me-more')]

如何编写xpath仅从父节点提取文本而不从其子节点提取文本?

2 个答案:

答案 0 :(得分:2)

通常您会使用//div[@id='blurb']/text()[1],但selenium不支持此语法,因为XPath应仅返回WebElement,而不是文本节点......

您可以使用JavaScriptExecutor获得所需的输出:

WebElement myDiv = driver.findElement(By.id("blurb"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
String divText = (String) jse.executeScript("return arguments[0].childNodes[0].nodeValue;", myDiv);

答案 1 :(得分:0)

我在xpath下面找到了答案并且它有效: -

//div[@id='blurb']/*[not(self::a)]

在这里,我们在父 div 标记

中省略内部锚标记 a
相关问题