Home > Software engineering >  Selenium and non select dropdown list
Selenium and non select dropdown list

Time:04-22

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.

CodePudding user response:

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

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

CodePudding user response:

The Filter by programme dropdown list opens up on clicking on the <label> element.


Solution

To click and expand the dropdown list you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using XPATH:

    driver.get("https://ec.europa.eu/info/funding-tenders/opportunities/portal/screen/opportunities/projects-results")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.wt-cck-btn-add"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[text()='Select a Programme...']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

ec_europa_eu

  • Related