Search code examples
seleniumselenium-webdriverxpathdropdownwebdriverwait

Selenium and non select dropdown list


Do you know if it was possible to click with selenium on a non select dropdown list?

I need to interact with this site : https://ec.europa.eu/info/funding-tenders/opportunities/portal/screen/opportunities/projects-results

And click on the "Filter by programme" and after scraping return to the 1st page.


Solution

  • Drop Down Navigation

    Elements within a dropdown list are typically hidden until the dropdown is initiated. Knowing this, the dropdown must be clicked on before clicking on any of the elements within the list. See below code for basic example:

    from selenium import webdriver
    
    driver = webdriver.Chrome()
    
    dropdown_list = driver.find_element_by_xpath("XPATH_OF_DROPDOWN_LIST")
    dropdown_list.click()
    
    dropdown_element = driver.find_element_by_xpath("XPATH_OF_DROPDOWN_ELEMENT")
    dropdown_element.click()
    

    WebDriverWait

    For a more advanced and better performing example, I would recommend the implementation of WebDriverWait as there sometimes is a lag between the elements of the dropdown list becoming available:

    dropdown_element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "XPATH_OF_DROPDOWN_ELEMENT")))
    

    Additional Resource

    For those unfamiliar, the difference between time.sleep and WebDriverWait is that time.sleep will always wait N number of seconds where N is time.sleep(N). WebDriverWait however, will wait up to N seconds where N is WebDriverWait(driver, N). Meaning if WebDriverWait variable is set to 10 seconds, but the element becomes available in 2, the driver will perform the action in the 2 seconds and move onto the next command. But if the element takes longer than 10 seconds to show up, the program will throw a timeout exception.