【问题标题】:Cannot select an option from drop down menu Python Selenium无法从下拉菜单 Python Selenium 中选择选项
【发布时间】:2017-03-23 02:58:13
【问题描述】:

我对 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")

但再次出现上述超时异常 如何进行 ? 我希望问题足够清楚,并提前谢谢大家!

【问题讨论】:

    标签: python selenium select drop-down-menu option


    【解决方案1】:

    您正在尝试处理错误的元素。试试这个代码,如果有任何问题,请告诉我:

    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()
    

    【讨论】:

    • 这完美地解决了我的问题!好样的!
    【解决方案2】:

    您链接到的站点的问题是选择框仅由 JS 管理,并且已被非选择下拉列表所取代。如果您想应用并非始终可用于操作系统定义的选择行为的自定义 UX,这很常见。

    您可以让 Selenium 执行一些 Javascript,从选择中删除 display:none 样式,然后执行您的选项选择。

    driver.execute_script("document.getElementById('criteriaSidesLeg').setAttribute('style','')");
    

    或者模拟对伪选择元素的必要点击,让网站 JS 为您更新选择。

    【讨论】:

      猜你喜欢
      • 2018-02-17
      • 1970-01-01
      • 2020-12-11
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2023-03-25
      相关资源
      最近更新 更多