硒CSS选择器,用于td跨度的第n次出现:nth-​​child(2) [英] Selenium CSS selector for nth occurrence of td span:nth-child(2)

查看:52
本文介绍了硒CSS选择器,用于td跨度的第n次出现:nth-​​child(2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

css选择器 td span:nth-​​child(2)表示 td 的第二个子节点 span .我想选择第n个 td span:nth-​​child(2),例如:

The css selector td span:nth-child(2) means the 2nd child node span of td. I wanna choose the nth td span:nth-child(2), something like:

driver.find_element_by_css_selector("td span:nth-child(2):eq(4)")

我知道我可以使用

driver.find_elements_by_css_selector("td span:nth-child(2)")[4]

或xpath:

driver.find_elements_by_xpath('(//td/span[2])[4]')

我只是想知道我是否可以使用CSS选择器做同样的事情.

I just wanna know if I can do the same thing with css selector.

推荐答案

您不能使用CSS选择器执行此操作.:eq()来自jQuery,不属于任何标准.

You can't do this with a CSS selector. :eq() is from jQuery and not part of any standard.

td:nth-​​child(4)span:nth-​​child(2)表示完全不同的东西,并且仅在非常特殊的情况下才起作用,例如当一个表行正好包含四个 td 元素,它们全部包含至少两个 span 子元素:

td:nth-child(4) span:nth-child(2) means something entirely different, and will only work in very specific situations, such as when there is exactly one table row with four td elements all of which contain at least two span children:

<body>
  ...
  <table>
    <!-- The first, or only, row in the entire document
         with this many cells containing this many spans -->
    <tr>
      <td>
        <span></span>
        <span></span>
      <td>
        <span></span>
        <span></span>
      <td>
        <span></span>
        <span></span>
      <td>
        <span></span>
        <span></span>
  </table>
  ...
</body>

它与您在此示例中查找的元素不匹配,因为每个 tr 仅具有两个 td 子元素,因此 td:nth-child(4)将永远不匹配:

It won't match the element you're looking for in this example, because each tr has only two td children, so td:nth-child(4) will never match:

<body>
  ...
  <table>
    <tr>
      <td>
        <span></span>
        <span></span>
      <td>
        <span></span>
        <span></span>
    <tr>
      <td>
        <span></span>
        <span></span>
      <td>
        <span></span>
        <span></span> <!-- (//td/span[2])[4] -->
  </table>
  ...
</body>

如果您知道 td:nth-​​child(4)span:nth-​​child(2)可以在您遇到的情况下正常工作,请随时使用它.

If you know td:nth-child(4) span:nth-child(2) is guaranteed to work in your situation though, feel free to use it.

这篇关于硒CSS选择器,用于td跨度的第n次出现:nth-​​child(2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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