简体   繁体   中英

Python - Selenium - select an element from a dropdown menu

I need to select an element from a dropdown menu.

For example:

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

but i have always some kind of error.

My code is so easy:

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

but it don´t work. It appears a message about "element not visible: Element is not currently visible and may not be manipulated"

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

any idea? i was trying with some fixes from another stackoverflow questions but i didn´t find the way...

The drop down that you tries to interact is not visible to the user unless we scroll up and that's why the error. Try to scroll up and then interact with the drop down. The below code might give you some idea.

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

element.location_once_scrolled_into_view

Hope this helps. Thanks.

The dropdown you are trying to select from is actually not the element with rangeFilter id. Its in the sibling <div> .

Since you can't use Select class on any tag either than <select> you need to first click on the dropdown to make it open the options and then click on the option

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

You can replace implicit waits by explicit wait which waits until the element is rendered in 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM