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