【问题标题】:Message: Element <option> could not be scrolled into view while trying to click on an option within a dropdown menu through Selenium消息:尝试通过 Selenium 单击下拉菜单中的选项时,元素 <option> 无法滚动到视图中
【发布时间】:2018-12-17 10:33:35
【问题描述】:

我正在尝试选择一个下拉菜单并选择一个选项。我正在使用最新版本的 Selenium、最新版本的 Firefox、最新版本的 geckodriver 和最新版本的 Python。

这是我的问题:当我尝试选择一个选项时,它给了我以下错误:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view.

我尝试了各种方法来解决这个问题,但似乎都没有奏效。以下是我尝试过的一些方法。

mySelectElement = browser.find_element_by_id('providerTypeDropDown')
dropDownMenu = Select(mySelectElement)
dropDownMenu.select_by_visible_text('Professional')

mySelectElement = browser.find_element_by_id('providerTypeDropDown')
dropDown = Select(mySelectElement)
for option in dropDown.options:
    message = option.get_attribute('innerText')
    print(message)
    if message == 'Professional':
        print("Exists")
        dropDown.select_by_visible_text(message) 
        break

element = browser.find_element_by_id('providerTypeDropDown')
browser.execute_script("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", element, "Professional")

HTML 代码遵循通常的选择标签和选项标签。任何帮助表示赞赏。 HTML 代码包含在下面。

<select data-av-chosen="providerTypes" id="providerTypeDropDown" data-placeholder="Please Select a Provider Type" name="providerTypeDropDown"
class="chzn-select input-full ng-pristine chzn-done ng-invalid ng-invalid-provider-type" data-ng-options="providerType.value for providerType in request.models.providerTypes"
data-ng-model="request.models.providerType" data-av-validator-field="providerType" data-disable-search-threshold="5" style="display; none;">
    <option value="" class="">Please Select a Provider Type</option>
    <option value="0">Professional</option>
    <option value="1">Institutional</option>
</select> 

打印语句用于测试/代码跟踪。

【问题讨论】:

    标签: python selenium selenium-webdriver drop-down-menu webdriverwait


    【解决方案1】:

    此错误消息...

    selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view.
    

    ...暗示您的程序试图与之交互的&lt;option&gt; 项目无法滚动到视图中。

    所需元素的 HTML 会给我们一些关于错误背后的想法。但是,似乎所需的元素不是Viewport 中的clickable /。要解决该问题,您必须诱导 WebDriverWait 以使 元素可点击,您可以使用以下解决方案:

    mySelectElement = browser.find_element_by_id('providerTypeDropDown')
    dropDownMenu = Select(mySelectElement)
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='providerTypeDropDown']//options[contains(.,'Professional')]")))
    dropDownMenu.select_by_visible_text('Professional')
    

    注意:您必须添加以下导入:

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

    【讨论】:

    • 你好,这仍然给我一个超时异常。如果有帮助,我已经添加了 HTML sn-p。
    • 您好!我使用 ActionChains 解决了这个问题。谢谢!
    • @S.Srikanth 以上是否表示所选答案并未真正解决您的问题?
    【解决方案2】:

    尝试添加等待:

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

    它将等待至少 10 秒,直到元素可点击,然后点击。

    我在您的代码中也没有看到您单击下拉按钮,该按钮会打开下拉菜单。找到此按钮,还添加一个等待并单击它,然后再选择该选项。希望对您有所帮助。

    注意:对于此代码,您必须添加一些导入:

    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    

    【讨论】:

    • 你好,这仍然给我一个超时异常。如果有帮助,我已经添加了 HTML sn-p。
    • 您好!我使用 ActionChains 解决了这个问题。谢谢!
    • @S.Srikanth 你能在这里分享你的解决方案吗
    • 本主题的未来读者。使用 Selenium python 绑定的鼠标悬停 (stackoverflow.com/a/8261754/9083077) 描述了使用 ActionChains 的解决方案。
    猜你喜欢
    • 2019-08-09
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 2019-06-19
    相关资源
    最近更新 更多