Search code examples
pythonselenium-webdriverxpathcss-selectorswebdriverwait

How to select an option form the non Select dropdown


URL: https://www.service-public.fr/simulateur/calcul/Dates_Vacances_Scolaires

I desperately try to select a department with selenium, for example 06 - Alpes-Maritimes so that I can finally choose the school year by finally recovering the dates, but unfortunately I can't find the right method, I access the select and option but despite many try nothing happens "Element not be scrolled into view" then if someone can help me it's cool.

sel = driver.find_element(By.XPATH, "//select[@id='Departement']")
sel.send_keys('06 - Alpes-Maritimes')
sel.submit


Solution

  • The clickable element with text as 06 - Alpes-Maritimes is within a <div>

    <div class="autocomplete-suggestion" data-val="06 - Alpes-Maritimes" data-value="9">
        ...
        ...
        ...
    </div>
    

    Alpes


    Solution

    The desired element is a dynamic element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

    • Using CSS_SELECTOR:

      driver.get("https://www.service-public.fr/simulateur/calcul/Dates_Vacances_Scolaires")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#AutocompletionListbox-Departement"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.autocomplete-suggestions div[data-val$='Alpes-Maritimes']"))).click()
      
    • Using XPATH:

      driver.get("https://www.service-public.fr/simulateur/calcul/Dates_Vacances_Scolaires")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='AutocompletionListbox-Departement']"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='autocomplete-suggestions ']//div[contains(@data-val, 'Alpes-Maritimes')]"))).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:

    maritimes