【问题标题】:How can i extract what is on the second <td>, CAT5 is what I need to extract in to a xpath我如何提取第二个 <td> 上的内容,CAT5 是我需要提取到 xpath 中的内容
【发布时间】:2021-09-27 02:03:39
【问题描述】:
<table class="">
 <tbody class>
   <tr class>
   <tr class>
   <tr class>
     <td>
      <span>Series</span>
     <td>CAT5</td>

tr 和 td 的数量可以不同,因为有多个表,我需要这样的 xpath 格式:

//table[@class="info-table specifications-table"]/tbody/tr/td/span[contains(text(),'Series')]/....

我试过这样但是tr顺序每次都不一样,唯一不变的是值总是在表的Series行上

//table[@class="info-table specifications-table"]/tbody/tr[8]/td[2]

Table looks like this and the Series can be on different position from top to bottom

【问题讨论】:

  • 你尝试了什么?什么不起作用?
  • 这是我尝试过但没有工作的,因为 td 不是 span //table[@class="info-table specifications-table"]/tbody/tr/td/span[contains(text(),'Series')]/following-sibling::td 的兄弟
  • 添加一个parent selector then? 可能类似于//table[@class="info-table specifications-table"]/tbody/tr/td/span[contains(text(),'Series')]/parent::td/following-sibling::td
  • 它正在工作,非常感谢您的帮助

标签: xpath xpath-2.0 xpath-1.0


【解决方案1】:

您可以使用textContent(或innerHTML)来获得所需的结果:

解决这个问题的 xpath 是使用parent selector like described here:

//table[@class="info-table specifications-table"]/tbody/tr/td/span[contains(text(),'Series')]/parent::td/following-sibling::td

document.querySelectorAll('tr td:nth-child(2)').forEach(td =&gt; console.log(td.textContent))
<table class="">
  <tbody class>
    <tr class>
      <td>
        <span>Series</span>
      </td>
      <td>CAT5</td>
    </tr>
    <tr class>
      <td>
        <span>Series</span>
      </td>
      <td>CAT1</td>
    </tr>
  </tbody>
</table>

【讨论】:

  • 我需要 xpath 如下所示: //table[@class="info-table specification-table"]/tbody/tr/td/span[contains(text(),'Series ')]/....
  • 这与您在问题中发布的内容完全不同。请使用我们需要的所有信息更新您的问题。
猜你喜欢
  • 1970-01-01
  • 2015-10-11
  • 2017-01-15
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多