【问题标题】:Unable to access Bootstrap dropdown with selenium in python无法在 python 中使用 selenium 访问 Bootstrap 下拉菜单
【发布时间】:2020-11-16 22:09:21
【问题描述】:

我的前端有一个下拉菜单,如下所示:

    <div class="dropdown-menu dropdown-menu-right text-left" id="dropdown_menu">
          <a class="dropdown-item" data-toggle="modal" data-target="#exampleModalCenter" id="dropdown_change_pwd">Change Password</a>
          <a class="dropdown-item" href="{{ url_for('auth.logout') }}" id="dropdown_logout">Logout</a>
    </div>

我想在 python 中使用 selenium 访问这些按钮。我尝试这样访问它:

class SearchPage:
_CHANGE_PWD_DROPDOWN = (By.ID, 'dropdown_change_pwd')
_LOGOUT_DROPDOWN = (By.ID, 'dropdown_logout')
_DROPDOWN = (By.ID, 'dropdown_menu')

def __init__(self, browser):
    self.browser = browser

def logout(self):
    time.sleep(5)
    dropdown = self.browser.find_element(*self._DROPDOWN)
    dropdown.click()
    logout = self.browser.find_element(*self._LOGOUT_DROPDOWN)
    logout.click()

但我总是以:

response = {'status': 400, 'value': '{\n\t"value" : \n\t{\n\t\t"error" : "element not interactable",\n\t\t"message" : "Element is not displayed",\n\t\t"stacktrace" : ""\n\t}\n}\r\n'}

Selenium 似乎无法处理引导下拉菜单。我怎样才能仍然单击此下拉列表中的任何元素?

如果没有直接的解决方案,是否有解决方法?

【问题讨论】:

    标签: python selenium bootstrap-4


    【解决方案1】:

    要点击 Logout 元素,您必须为element_to_be_clickable() 诱导WebDriverWait,您可以使用以下Locator Strategies 之一:

    • 使用CSS_SELECTOR

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.dropdown-menu.dropdown-menu-right.text-left"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.dropdown-item#dropdown_logout"))).click()
      
    • 使用XPATH

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='dropdown-menu dropdown-menu-right text-left']"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='dropdown-item' and @id='dropdown_logout']"))).click()
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      

    【讨论】:

    • 感谢您的回答。但是,这两者都会导致 TimeoutException。
    • @MarcB。您甚至没有在答案中使用的问题中提供锚元素。
    • 你是对的。你没有办法回答我的问题。后来我意识到了。
    【解决方案2】:

    我的下拉菜单看起来像这样:

        <li class="nav-item">
           <a class="nav-link dropdown-toggle" id="welcome_user" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Welcome, {{ session['current_user']['name'] }}</a>
           <div class="dropdown-menu dropdown-menu-right text-left">
              <a class="dropdown-item" data-toggle="modal" data-target="#exampleModalCenter" id="dropdown_change_pwd">Change Password</a>
              <a class="dropdown-item" href="{{ url_for('auth.logout') }}" id="dropdown_logout">Logout</a>
           </div>
        </li>
    

    首先我必须访问 upper 元素:

       dropdown = self.browser.find_elemnt(By.ID, 'welcome_user')
       dropdown.click()
    

    然后我可以像这样访问链接:

        dropdown_logout = self.browser.find_elemnt(By.ID, 'dropdown_logout')
        dropdown_logout.click()
    

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2018-04-02
      • 2017-06-16
      • 2023-04-01
      • 1970-01-01
      • 2016-11-16
      • 2018-01-31
      相关资源
      最近更新 更多