【问题标题】:Can't click on dropdown button selenium Python无法单击下拉按钮 selenium Python
【发布时间】:2019-12-17 22:26:08
【问题描述】:

我想点击元素“项目”以显示下拉列表(见下图) 在其他地方使用 python 中的 selenium 库时出现错误:

could not be scrolled into view

例如,使用以下代码获得:

driver = webdriver.Firefox()
driver.get(url)
driver.find_element_by_xpath('//div[@class="multiselect-container"]').click()

或者在我等待元素显示的地方使用一些代码,例如这里描述的: Message: Element <option> could not be scrolled into view while trying to click on an option within a dropdown menu through Selenium

mySelectElement = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "edit-projects")))
mySelectElement.click()

但我无法让它工作。

任何将不胜感激。

html源代码可以在这里找到:

https://rpidejr.hopto.org/f/8a909b51dcc34d09a00a/

【问题讨论】:

    标签: python html selenium firefox button


    【解决方案1】:

    我将源代码文件作为 test.html 保存到我的驱动器并打开它并单击带有以下代码的项目框。只需编辑本地计算机的路径即可。

    from selenium import webdriver
    import time
    
    driver = webdriver.Firefox(executable_path=r'C:\\Path\\To\\geckodriver.exe')
    
    driver.get("file:///C:/Path/To/test.html")
    
    time.sleep(1)
    #project = driver.find_element_by_xpath("//select[@id='edit-projects']")
    #project.click()
    
    project_elements = driver.find_elements_by_xpath("//select[@id='edit-projects']")
    for element in project_elements:
        try:
            element.click()
        except Exception as e:
            print(e)
    

    【讨论】:

    • 是的,它确实在本地工作。但是,当我想尝试“在线”时,会出现以下代码:“ selenium.common.exceptions.ElementNotInteractableException: Message: Element
    • 确实有一个具有相同 id 的元素(按钮“Catégories de tâches”)。我再次收到错误消息:消息:元素
    • 我更新了包含两个id的源文档
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签