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

前端之家收集整理的这篇文章主要介绍了html – 如何在Selenium WebDriver中获取单个文本节点前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Getting text from a node2个
我想从标签获取文本但没有嵌套标签的文本.即在下面的示例中,我只想从< small>中获取字符串183591.从< span>标记并排除文本服务请求ID:标签.这不是微不足道的,因为< span> tag嵌套在< small>中标签.这可能与WebDriver和XPath一起使用吗?

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

  1. <div id="claimInfoBox" style="background-color: transparent;">
  2. <div class="col-md-3 rhtCol">
  3. <div class="cib h530 cntborder">
  4. <h4 class="no-margin-bottom">
  5. <p>
  6. <small style="background-color: transparent;">
  7. <span class="text-primary" style="background-color: transparent;">Service Request ID:</span>
  8. 183591
  9. </small>
  10. </p>
  11. <div class="border-bottom" style="background-color: transparent;"></div>
  12. <div id="CIB_PersonalInfo_DisplayMode" class="cib_block">
  13. <div id="CIB_PersonalInfo_EditMode" class="cib_block" style="display: none">
  14. </div>
  15. </div>
  16. <script type="text/javascript">
  17. </div>
  18. </div>

解决方法

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

猜你在找的HTML相关文章