加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

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

发布时间:2020-12-14 23:33:14 所属栏目:资源 来源:网络整理
导读:参见英文答案 Getting text from a node2个 我想从标签中获取文本但没有嵌套标签的文本.即在下面的示例中,我只想从 small中获取字符串183591.从 span标记并排除文本服务请求ID:标签.这不是微不足道的,因为 span tag嵌套在 small中标签.这可能与WebDriver和X
参见英文答案 > 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,"");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读