我正在尝试使用XPath&Java在<br>
标记之前获取文本。我尝试了多种解决方案,但还没有走运。
//div[@class="help-block"]/p[count(preceding-sibling::br) < 1]
这是我的HTML代码:
<div class="help-block">
<p>
This is sample text
<br>
Text after a breakpoint
</p>
</div>
预期结果:This is sample text
。
实际结果:This is sample text Text after a breakpoint
答案 0 :(得分:0)
我认为您可以运行以下命令:
driver.find_element_by_xpath("//p[br]").text
这将获得<p>
元素,但查询还会检查以确保<p>
内包含<br>
元素。因此,您只是得到<p>
,而.text
应该从<p>
得到文本。
答案 1 :(得分:0)
您的表情很接近。但是您没有考虑到p
只是一个元素,而您想要p
的子节点。因此,将您的表达式更改为以下内容(还转义了<
字符)
//div[@class="help-block"]/p/node()[count(preceding-sibling::br) < 1]
其输出为:
This is sample text
答案 2 :(得分:0)
获取This is sample text
并生成WebDiverWait
和visibilityOfElementLocated()
并获取元素。
得出JavascriptExecutor
并得到textContent
的{{1}}
firstChild