首页 > 解决方案 > Select is not iterable error while select is not iterable error while select a Dropdown option based on user input using Selenium and Python

问题描述

每次用户输入公司名称时,我都想从用户那里获取输入。所以我收到此错误是否有其他方法可以解决此查询?

from selenium.webdriver.support.ui import Select
from selenium import webdriver as driver
menu = Select(driver.find_element_by_id('BodyContent__company'))
name=input('Enter company name: ')
for option in menu:
    if option.text == name:
        option.select()
    else:
        pass

错误:

Select is not iteratable

标签: pythonseleniumselenium-webdriverdrop-down-menuhtml-select

解决方案


此错误消息...

Select is not iteratable

...意味着在您的代码块中,您正在尝试遍历type 的WebElementSelect


解决方案

您需要遍历元素,而不是迭代Select.options元素,如下所示:

from selenium.webdriver.support.ui import Select
from selenium import webdriver as driver

menu = Select(driver.find_element_by_id('BodyContent__company'))
name=input('Enter company name: ')
for option in menu.options:
    if option.text == name:
        option.click()
    else:
        pass

推荐阅读