Html XPath:: 获得以下兄弟姐妹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11657223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:49:31  来源:igfitidea点击:

XPath:: Get following Sibling

htmlxpathsiblingsscraper

提问by add-semi-colons

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.

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

我正在尝试提取具有解码值的第二个“颜色摘要”td 元素。

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

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

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

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

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

回答by james31rock

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.

您应该寻找具有等于“颜色摘要”的 td 的第二个 tr,然后您需要查看 tr 中第一个 td 的以下同级,或第二个 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

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

回答by Dhaval Jethava

You can go for identifying a list of elements with xPath:

您可以使用 xPath 识别元素列表:

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

This will give you a list of two elements, than you can use the 2nd element as your intended one. For example:

这将为您提供两个元素的列表,然后您可以将第二个元素用作您想要的元素。例如:

List<WebElement> elements = driver.findElements(By.xpath("//td[text() = ' Color Digest ']/following-sibling::td[1]"))

Now, you can use the 2nd element as your intended element, which is elements.get(1)

现在,您可以使用第二个元素作为您想要的元素,即elements.get(1)

回答by guanome

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

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

In Chrome (possible Safari too) you can inspect an element, then right click on the tag you want to get the xpath for, then you can copy the xpath to select that element.

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