【问题标题】:Selenium python, use css to select only two classSelenium python,使用css只选择两个类
【发布时间】:2021-08-26 16:55:37
【问题描述】:

假设我有一个如下所示的 HTML:

<div class="first second">
    Right!
</div>
<div class="first second third fourth">
    Wrong!
</div>
<div class="first second">
    Right!
</div>

如果我尝试使用 css 定位第一个和第三个 div:

driver.find_elements_by_class_name("first, second")

我没有在我的列表中获得 2 个元素,而是获得了所有三个 div,因为第二个 div 实际上包含这些类和其他一些......我该如何限制它,只找到 &lt;div&gt;s :

class = "first second"

【问题讨论】:

  • 你有什么标签包含first, second 第三个标签也包含同样的东西。
  • 你不能使用 Xpath?

标签: python css selenium class classname


【解决方案1】:

如果您需要通过 xpath 获取它们,可以使用它:

driver.find_elements_by_xpath(".//div[@class='first second']")

【讨论】:

    【解决方案2】:
    div[class = 'first second']
    

    应该可以。因为它是完全匹配的。

    CSS_SELECTOR一起使用

    driver.find_elements(By.CSS_SELECTOR , "div[class = 'first second']")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 2017-10-16
      • 2019-09-14
      • 1970-01-01
      • 2013-01-13
      • 2013-09-07
      相关资源
      最近更新 更多