通过 XPath 获取 tr 中特定 td 的索引 [英] Get index of the specific td in tr by XPath

查看:48
本文介绍了通过 XPath 获取 tr 中特定 td 的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在桌子上有 thead 和 tbody.thead 中包含几个 s.每一个都有一个id.我需要通过id在tad中找到td的索引,然后在tbody中通过索引找到.

I have thead and tbody in the table. Thead contains a few s in . Each of it have an id. I need to find the index of td in thead by id and then find by index in tbody.

<table>
  <thead>
   <tr>
     <td data-date="2019-08-05"></td>
     <td data-date="2019-08-06"></td> //find index of this element
     <td data-date="2019-08-07"></td>
   </tr>
  </thead>
  <tbody>
   <tr>
    <td>aaa</td>
    <td>bbb</td> //find this element by found index
    <td>ccc</td>
   </tr>
  </tbody>
</table>
 upd
<table>
  <thead>
   <tr>
     <td data-date="2019-08-05"></td>
     <td data-date="2019-08-06"></td> 
     <td data-date="2019-08-07"></td> 
     <td data-date="2019-08-08"></td> //find index of this element
     <td data-date="2019-08-09"></td>
   </tr>
  </thead>
  <tbody>
   <tr>
    <td rowspan="2"></td>
    <td rowspan="2" class="rrrr">event1 2019-08-06</td>
    <td class="rrrr">event1 2019-08-07</td>
    <td class="rrrr"event1 2019-08-08</td> //find this element by found index
    <td rowspan="2"></td>
   </tr>
   <tr>
    <td class="rrrr">event2 2019-08-07</td>
    <td class="rrrr">event2 2019-08-08</td> //find this element by found index
   </tr>
  </tbody>
</table>

推荐答案

这个 xpath 表达式

This xpath expression

//tbody//td[count(//thead//td[@data-date='2019-08-06']/preceding-sibling::*)+1]

选择

<td>bbb</td>

这篇关于通过 XPath 获取 tr 中特定 td 的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆