XPath ::获取以下兄弟 [英] XPath:: Get following Sibling

查看:186
本文介绍了XPath ::获取以下兄弟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML结构:我试图构建一个强大的方法来提取第二个颜色摘要元素,因为DOM中会有很多这样的标签。

 <表> 
< tbody>
< tr bgcolor =#AAAAAA>
< tr>
< tr>
< tr>
< tr>
< td>色彩摘要< / td>
< td> AgArAQICGQMVBBwTIRQHIwg0GUMURA​​ZTBWQJcwV0AoEDAQ< / td>
< / tr>
< tr>
< td>色彩摘要< / td>
< td> 2,43,2,25,21,28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,33, 7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,6,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,2,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,< / td>
< / tr>
< / tbody>
< / table>

我试图提取具有解码值的第二个Color Digesttd元素。



我写了下面的xpath,而不是第二个我没有得到第二个td元素。

  // td [text()='Color Digest'] / following-sibling :: td [2] 

当我将它更改为td [2]到td [1]时,我得到了这两个元素。

解决方案

你应该寻找具有等于'Color Digest'的td的第二个tr,那么你需要查看tr中第一个td的下一个兄弟或者第二个td。 / p>

请尝试以下操作:

  // tr [td ='Color Digest'] [2] / td / following-sibling :: td [1] 

p>

  // tr [td ='Color Digest'] [2] / td [2] 

http://www.xpathtester.com/saved/76bb0bca-1896-43b7-8312-54f924a98a89


I have following HTML Structure: I am trying to build a robust method to extract second color digest element since there will be many of these tag within the DOM.

<table>
  <tbody>
    <tr bgcolor="#AAAAAA">
    <tr>
    <tr>
    <tr>
    <tr>
      <td>Color Digest </td>
      <td>AgArAQICGQMVBBwTIRQHIwg0GUMURAZTBWQJcwV0AoEDAQ </td>
    </tr>
    <tr>
      <td>Color Digest </td>
      <td>2,43,2,25,21,28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,33,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, </td>
    </tr>
  </tbody>
</table>

I am trying to extract the Second "Color Digest" td element that has the decoded value.

I wrote the following xpath but instead of getting the second i am not getting the second td element.

//td[text() = ' Color Digest ']/following-sibling::td[2]

And when I change it to td[2] to td[1] I get both the elements.

解决方案

You should be looking for the second tr that has the td that equals ' Color Digest ', then you need to look at either the following sibling of the first td in the tr, or the second td.

Try the following:

//tr[td='Color Digest'][2]/td/following-sibling::td[1]

or

//tr[td='Color Digest'][2]/td[2]

http://www.xpathtester.com/saved/76bb0bca-1896-43b7-8312-54f924a98a89

这篇关于XPath ::获取以下兄弟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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