html – 如何在Selenium WebDriver中获取单个文本节点

参见英文答案 > Getting text from a node2个
我想从标签获取文本但没有嵌套标签的文本.即在下面的示例中,我只想从< small>中获取字符串183591.从< span>标记并排除文本服务请求ID:标签.这不是微不足道的,因为< span> tag嵌套在< small>中标签.这可能与WebDriver和XPath一起使用吗?

标签中的文字每次都会改变.

<div id="claimInfoBox" style="background-color: transparent;">
<div class="col-md-3 rhtCol">
<div class="cib h530 cntborder">
<h4 class="no-margin-bottom">
<p>
<small style="background-color: transparent;">
<span class="text-primary" style="background-color: transparent;">Service Request ID:</span>
183591
</small>
</p>
<div class="border-bottom" style="background-color: transparent;"></div>
<div id="CIB_PersonalInfo_displayMode" class="cib_block">
<div id="CIB_PersonalInfo_EditMode" class="cib_block" style="display: none">
</div>
</div>
<script type="text/javascript">
</div>
</div>

解决方法

您将不得不使用字符串操作.就像是:
// you will need to adjust these XPaths to suit your needs
String outside = driver.findElement(By.xpath("//small")).getText();
String inside = driver.findElement(By.xpath("//span")).getText();

String edge = outside.replace(inside,"");

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些