【问题标题】:Selenium WebDriver css selectorSelenium WebDriver css 选择器
【发布时间】:2021-05-27 13:02:16
【问题描述】:
<div class="card-body">
blah blah
</div>

<div class="card-body">
   <table class="table-striped">
      some a anchor
   </table>
</div>

<div class="card-body">
   <table class="table-striped">
      some a anchor
   </table>
</div>

我正在尝试从第三个卡体中获取锚点(使用 selenium python)。所以我试试这个

driver.find_elements_by_css_selector("div.card-body:nth-child(3)>table.table-striped a")

这不起作用

如果我使用下面的,

driver.find_elements_by_css_selector("div.card-body:nth-child(2)>table.table-striped a")

我从 2nd 和 3rd 都得到了所有的 a 锚。

如何只从第三个 div.card-body 中获取锚点?

【问题讨论】:

  • 为什么不使用python通过访问列表的索引进行过滤?

标签: python css selenium


【解决方案1】:

您可以尝试使用 x-path 来实现。

driver.find_elements_by_xpath("(//div[@class='card-body'])[2]//a")

【讨论】:

【解决方案2】:

试试这个 XPath:

'(//div[@class="card-body"])[3]//a'

【讨论】:

    【解决方案3】:

    使用last-of-type

    div.card-body:last-of-type a
    

    :nth-child(n) 选择器匹配其父元素的第 n 个子元素,无论其类型如何。

    阅读更多here

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 2013-08-01
    相关资源
    最近更新 更多