首页 > 解决方案 > 我在 selenium 'NoneType' 对象没有属性 'options' 上收到此错误

问题描述

select = driver.find_element_by_xpath('//select[@id="select_noti_segments_in"]').click()
for index in range(len(select.options)):
        select = driver.find_element_by_xpath('//select[@id="select_noti_segments_in"]')
        select.select_by_index(1)
        time.sleep(5)

对于范围内的索引(len(select.options)):AttributeError:'NoneType'对象没有属性'options' 我在运行python脚本时收到此错误我可能知道我为什么会收到此错误以及如何解决此问题

标签: python-3.xselenium

解决方案


只需clickselect定义中删除调用:

select = driver.find_element_by_xpath('//select[@id="select_noti_segments_in"]')

您还需要导入Select类并按如下方式使用它:

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_id('select_noti_segments_in'))
select.select_by_index(1)

推荐阅读