0

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

2
  • Have you tried select_by_value? Commented Jun 5, 2017 at 22:05
  • Y es. I tried but i had the same error. Thanks!!! Commented Jun 5, 2017 at 22:09

3 Answers 3

2

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Guy for your help! I didn´t work... I need to learn more about selenium. button type="button" class="btn dropdown-toggle btn-default" data-toggle="dropdo wn" data-id="rangeFilter" title="View by">...</button> is not clickable at point (182, 603). Other element would receive the click: <p class="cookie-disclosure- message hidden-xs">...</p>
1

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.

2 Comments

Is the page getting scrolled and drop down visible?
no. I´m not able.... my code: driver = webdriver.Chrome("D:\Python27\selenium\webdriver\chrome\chromedriver.exe") driver.implicitly_wait(5) driver.maximize_window() driver.get("ispspeedindex.netflix.com/country/norway/") driver.implicitly_wait(5) element=driver.find_element_by_xpath("//option[@value='all']") element.location_once_scrolled_into_view
0

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

1 Comment

thanks kiran for your support but it´s not work for me. I´ve the same error: selenium.common.exceptions.ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.