Select 使用 XPath 紧跟字体真棒图标之后的文本

Select text immediately after font awesome icons using XPath

我正在尝试提取未嵌套在 HTML 元素中的文本。这是 HTML

<div class="col-sm-12">
  <i class='fa fa-map-marker'></i>theCity
  <i class='fa fa-link'></i>theEmail
  <i class='fa fa-phone'></i>thePhone1
  <i class='fa fa-phone'></i>thePhone2
  <b>Fax:</b>theFax
  <b>Address:</b>theAddress
</div>

我想得到以下结果

如您所见,有不同的格式。 theCity、theEmail、thePhone1 和 thePhone2 具有相似的格式,而 theFax 和 theAddress 具有另一种格式。 我尝试使用以下语句获取这两种类型的数据,但没有成功。

这是我试过的传真和地址代码

//b/following-sibling::text()[1]

这是城市、电子邮件和 phone 数据类型的代码

normalize-space(//div[@class="fa-map-marker"]/following-sibling::text())

我做错了什么?

What am I doing wrong?

  1. 对于基于 i 的标签,请注意 fa fa-map-marker 类 在 i 上,而不是 div。此外,如果您希望使用相等性,则必须针对整个属性值进行测试。如果您希望使用 contains() 以获得更强大的解决方案,请参阅 XPath to match @class value and element value? 最后,不要忘记 [1] 以确保您只获得紧随其后的文本节点。
  2. 对于基于 b 的标签,请在使用 following-sibling:: 之前指定 b 元素的内容,因为您已经在正确地执行此操作。

以下是 select 每个目标的 XPath 表达式:

  • 城市: //i[@class="fa fa-map-marker"]/following-sibling::text()[1]
  • 邮箱: //i[@class="fa fa-link"]/following-sibling::text()[1]
  • 手机1: //i[@class="fa fa-phone"][1]/following-sibling::text()[1]
  • 手机2: //i[@class="fa fa-phone"][2]/following-sibling::text()[1]
  • 传真: //b[.="Fax:"]/following-sibling::text()[1]
  • 地址: //b[.="Address:"]/following-sibling::text()[1]