微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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,"");

原文地址:https://www.jb51.cc/html/228486.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐