硒:动态选择错误选择选项

时间:2018-12-28 12:07:18

标签: python python-3.x selenium selenium-webdriver

我在python中使用硒。我不知道为什么当我尝试动态选择选择的选项时出现错误。这些是我的代码:

select_season = Select(driver.find_element_by_name("_ctl0:temporadasDropDownList"))
select_season.select_by_value("2015")

这些代码可以正常工作,但是如果我将值更改为“ 2015”,则从变量或列表中获取该错误:

  

回溯(最近通话最近):文件   “ /home/josecarlos/Workspace/python/europa/main.py”,第104行,在          main()在main中的文件“ /home/josecarlos/Workspace/python/europa/main.py”第94行       FEBDocument(data)文件“ /home/josecarlos/Workspace/python/europa/parsers/feb/document/bx_document.py”,   第21行,初始化       self.load_all_competitions(驱动程序,数据)文件“ /home/josecarlos/Workspace/python/europa/parsers/feb/document/bx_document.py”,   第114行,在load_all_competitions中       select_season.select_by_value(data [“ season”])文件“ /home/josecarlos/Workspace/python/europa/venv/lib/python3.5/site-packages/selenium/webdriver/support/select.py”,   第79行,在select_by_value中       opts = self._el.find_elements(By.CSS_SELECTOR,css)文件“ /home/josecarlos/Workspace/python/europa/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py”,   第685行,在find_elements中       {“使用”:通过,“值”:值})['值']文件“ /home/josecarlos/Workspace/python/europa/venv/lib/python3.5/site-packages/selenium/webdriver/remote/ webelement.py”,   _execute中的第633行       返回self._parent.execute(命令,参数)文件“ /home/josecarlos/Workspace/python/europa/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py”,   第321行,在执行中       self.error_handler.check_response(响应)文件“ /home/josecarlos/Workspace/python/europa/venv/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py”,   第242行,在check_response中       引发exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.InvalidSelectorException:消息:给定   CSS选择器表达式“ option [value =” 2015“]”无效:   语法错误:'option [value =“ 2015”]'不是有效的选择器

我收到此错误的代码是:

select_season = Select(driver.find_element_by_name("_ctl0:temporadasDropDownList"))
select_season.select_by_value(data["season"])

如何使用python和硒从select中动态选择一个选项?

0 个答案:

没有答案