Python Selenium - 2 个同名按钮

Python Selenium - 2 buttons with same name

我想点击一个按钮,但我无法点击,因为有 2 个按钮具有相似的名称 class,我无法点击我想要的按钮。

    Button 1: 
<button class="dropdown-trigger"> 
   <i class="icon2-arrow-down">
      ::before

    Button 2:
<button class="dropdown-trigger is visible-desktop"> 
   <i class="icon2-arrow-down arrow-icon">
      ::before

我只想点击按钮 1,你能帮帮我吗?

如果你用的是Chrome浏览器,(其他浏览器的开发者工具我不知道,但肯定有类似的东西。。。)轻松挑选独特的元素。

  1. 按'F12'键打开Chrome开发者工具。 (页面可能会刷新。)
  2. 右键单击要关注的按钮。
  3. 点击'Inspect'。
  4. 在浏览器左侧的开发者工具中,有些东西会发生变化,HTML 源代码上应该有一个灰色框。 (如果您将鼠标移到它上面,您检查过的按钮应该会突出显示)
  5. 右键单击灰色线,然后单击 'Copy - Copy Selector'。 (根据您的喜好,您可以复制其他功能来找到该元素)
  6. 您现在可以使用复制的选择器(或任何功能)准确找到您指定的一个元素。

如有任何疑问,请在此留言post。谢谢!

如果你尝试会发生什么

driver.find_element_by_css_selector(".dropdown-trigger.visible-desktop").click()

或者您可以尝试获取显示的两个元素中的元素...

buttons = driver.find_elements_by_class_name('dropdown-trigger')
button = next(filter(lambda x: x.is_displayed() == True, buttons))
button.click()