How to get a single text node in Selenium WebDriver(如何在 Selenium WebDriver 中获取单个文本节点)
问题描述
我想从标签中获取文本,但没有嵌套标签中的文本.IE.在下面的示例中,我只想从 <small>
标记中获取字符串 183591
并排除文本 Service Request ID:
来自 <span>
标签.这不是微不足道的,因为 <span>
标记嵌套在 <small>
标记中.这可以通过 WebDriver 和 XPath 实现吗?
I want to get the text from a tag but without the text from nested tags. I.e. in the example below, I only want to get the string 183591
from inside the <small>
tag and exclude the text Service Request ID:
from the <span>
tag. This is not trivial because the <span>
tag is nested in the <small>
tag. Is this possible with WebDriver and XPath?
标签中的文字每次都会改变.
The text in the tag is going to change every time.
<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 are going to have to use String manipulation. Something like:
// 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, "");
这篇关于如何在 Selenium WebDriver 中获取单个文本节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Selenium WebDriver 中获取单个文本节点


基础教程推荐
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01