python - 消息 : Element <option> could not be scrolled into view while trying to click on an option within a dropdown menu through Selenium

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

我正在尝试选择一个下拉菜单并选择一个选项。我正在使用最新版本的 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> 

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

最佳答案

这个错误信息...

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

...暗示 <option>您的程序试图与之交互的项目无法滚动到 View 中。

所需元素的 HTML 会给我们一些错误背后的想法。然而,似乎所需的元素不是 clickable/内Viewport .要解决该问题,您必须引入 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

关于python - 消息 : Element <option> could not be scrolled into view while trying to click on an option within a dropdown menu through Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51250668/

相关文章:

java - 方法中的参数数量

python - 如何通过Python使用Selenium从网页中提取文本$7.56

selenium-webdriver - 识别通过框架生成而不是通过原始 HTML 构建的对象

python - 二维 numpy 数组的条件数学运算检查一维并在不同维度上执行不同的操作

python - 我的自定义 Django 模板过滤器出现 "Invalid Filter"错误,但任何其他过滤器都没有

python - Django 模板标签返回 True 或 False

python - 使用 Python 和 Beautifulsoup4 在指定元素后提取链接 URL

java - Selenium WebDriver 在选择 xpath 时抛出错误

testing - 我们如何测试电子商务网站中图像缩放的存在

selenium - 如何检查标签是否为最后一个