Python - Selenium - 从下拉菜单中选择一个元素

时间:2017-06-05 21:54:15

标签: python selenium selenium-webdriver

我需要从下拉菜单中选择一个元素。

例如:

    <div class="col-sm-4 col-lg-2">
       <label for="rangeFilter" class="sr-only">Date Range</label>
       <select class="selectpicker" id="rangeFilter" data-none-selected-text="Range" name="range">
              <option value="">View by</option>
              <option value="6month">6 months</option>
              <option value="1year">1 Year</option>
              <option value="2year">2 Year</option>
              <option value="all">All time</option>
         </select>
     </div>

但我总是有某种错误。

我的代码非常简单:

driver = webdriver.Chrome("D:\Python27\selenium\webdriver\chrome\chromedriver.exe")
driver.implicitly_wait(5)
driver.maximize_window()
driver.get("https://ispspeedindex.netflix.com/country/norway/")
time.sleep(15)
select = Select(driver.find_element_by_id('rangeFilter'))
select.select_by_visible_text('All time')

但它不起作用。它显示一条消息“元素不可见:元素当前不可见,可能无法操作”

Traceback (most recent call last):
File "scraping.py", line 23, in <module>
select.select_by_visible_text('All time')
File "D:\Python27\lib\site-packages\selenium\webdriver\support\select.py", line 120, in select_by_visible_text
...
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated

任何想法?我试图从另一个stackoverflow问题的一些修复,但我没有找到方法......

3 个答案:

答案 0 :(得分:1)

除非我们向上滚动,否则您尝试进行交互的下拉菜单对用户不可见,这就是错误原因。尝试向上滚动,然后与下拉菜单进行交互。以下代码可能会给你一些想法。

element=find_element_by_xpath("xpath of the element you are trying to access")

element.location_once_scrolled_into_view

希望这会有所帮助。感谢。

答案 1 :(得分:1)

您尝试从中选择的下拉菜单实际上不是rangeFilter ID的元素。它在兄弟姐妹<div>

由于您无法在Select以外的任何标记上使用<select>类,因此您需要首先点击下拉菜单以打开选项,然后点击选项

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome("D:\Python27\selenium\webdriver\chrome\chromedriver.exe")
driver.implicitly_wait(5)
driver.maximize_window()
driver.get("https://ispspeedindex.netflix.com/country/norway/")

drop_down = driver.find_element_by_css_selector('[data-id="rangeFilter"]')
ActionChains(driver).move_to_element(drop_down).perform()
drop_down.click()

option = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//span[contains(., "All time")]')))
option.click()

答案 2 :(得分:0)

您可以通过显式等待替换隐式等待,等待直到元素在DOM中呈现。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome("D:\Python27\selenium\webdriver\chrome\chromedriver.exe")
driver.maximize_window()
driver.get("https://ispspeedindex.netflix.com/country/norway/")
try:
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "rangeFilter"))
    )
    select = Select(driver.find_element_by_id('rangeFilter'))
    select.select_by_visible_text('All time')
finally:
    driver.quit()