无法从下拉菜单Python Selenium中选择一个选项

出色地

我对Python比较陌生(嗯,比相对而言还多)。我需要从下拉菜单中选择一个选项。我已经尝试了几乎所有可用的解决方案。但是似乎没有任何作用。这是我正在与之交互的页面:http : //www.europarl.europa.eu/plenary/en/debates-video.html?action=1 & tabActif= tabResult# sidesForm这是页面源代码中的一部分我的问题:

<select id="criteriaSidesLeg" name="leg" style="display:none;" aria-disabled="false">

                    <option title="2014 - 2019" value="8">2014 - 2019</option>

                    <option title="2009 - 2014" value="7" selected="selected">2009 - 2014</option>

                    <option title="2004 - 2009" value="6">2004 - 2009</option>

                    <option title="1999 - 2004" value="5">1999 - 2004</option>

</select>

我到目前为止尝试过的是:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
# pressing the botton year menu making the element visible
elem_year_arrow=driver.find_element_by_id("criteriaSidesLeg-button")
elem_year_arrow.click()
year= driver.find_element_by_id('criteriaSidesLeg')
for option in year.find_elements_by_tag_name('option'):
    if option.text=='2009 - 2014':
        option.click()
        break

这给了我这个错误:

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated
  (Session info: chrome=54.0.2840.87)
  (Driver info: chromedriver=2.25.426935 (820a95b0b81d33e42712f9198c215f703412e1a1),platform=Mac OS X 10.10.5 x86_64)

我也尝试过其他解决方案

wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.ID,"criteriaSidesLeg")))
select = Select(element)
select.select_by_value('7')

这不是给我错误,而是这个TimeOutexception

raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

因此,我也尝试执行以下命令行:

driver.execute_script("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].value == arguments[1]){ select.options[i].selected = true; } }", element, "01")

但是再次获得上述超时异常如何进行?我希望这个问题已经足够清楚,在此先感谢大家!

安德森

您正在尝试处理错误的元素。尝试使用此代码,如有任何问题,请通知我:

driver.find_element_by_xpath('//a[@id="criteriaSidesLeg-button"]').click()
driver.find_element_by_xpath('//a[text()="2009 - 2014"]').click()

相同但不使用XPath

driver.find_element_by_id('criteriaSidesLeg-button').click()
driver.find_element_by_link_text('2009 - 2014').click()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Selenium和Python从下拉菜单中选择一个选项

使用selenium和python从下拉菜单中选择一个选项

无法使用 Python、Selenium 从下拉列表中选择一个选项

无法从下拉菜单中选择一个选项,因为我有2个下拉菜单,且具有相同的'<select class =

Python - 从下拉菜单中选择选项

Python Selenium - 从下拉菜单中选择值

检查用户是否从下拉菜单中选择一个选项

如何从下拉菜单中选择一个选项

当下拉菜单具有相同的ID时,如何从下拉菜单中选择一个选项

无法从下拉菜单中选择值

使用Selenium Python从下拉选项中选择一个值

python硒从下拉菜单中选择

当用户从下拉菜单中选择一个选项时,如何更新任何归档的文本?

Javascript:发现从下拉菜单中选择一个选项后会触发什么功能

从下拉菜单中选择值

从下拉菜单中选择元素

下拉选择的选项在另一个下拉菜单中选择选项

使用python硒从Google表单下拉菜单中选择一个选项

允许用户从下拉菜单中选择选项

从下拉菜单中选择选项后的事件

RSelenium:从下拉菜单中选择选项

如何从python selenium的下拉菜单中选择一个值

Selenium没有从下拉列表中选择一个选项

Geb:如何从下拉菜单中选择一个值?

从下拉菜单jquery中选择一个表单

通过Javascript从下拉菜单中选择一个值[Google帐户创建页面]

如何使用Selenium和Python通过部分文本从下拉菜单中选择选项

如何通过Selenium和python从下拉菜单中选择元素?

使用Python + Selenium选择一个下拉菜单