XPath ::获取以下兄弟

我有以下的HTML结构:我想build立一个强大的方法来提取第二个颜色摘要元素,因为在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> 

我正试图提取具有解码值的第二个“Color Digest”td元素。

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

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

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

你应该找第二个tr,它的td等于“Color Digest”,那么你需要看tr的第一个td的下一个兄弟,或者第二个td。

尝试以下操作:

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

要么

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

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

/html/body/table/tbody/tr[9]/td[1]

在Chrome(也可能是Safari)中,您可以检查一个元素,然后右键单击要获取xpath的标记,然后可以复制xpath以select该元素。

    Interesting Posts