1

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
spookyz
  • 13
  • 3
  • Can you add enough code that we can reproduce what you're doing - i.e., the imports, the code that fetches the page, every needed so I can cut and paste your code on my machine and see what happens? – joanis Jun 10 '23 at 20:59

1 Answers1

1

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thank you very much for your high-quality assistance. I had started looking into EC, but I still haven't fully grasped how it works. A big thank you as well for sharing your knowledge and providing learning resources. I don't have enough points to add +1 to your answer, sorry. – spookyz Jun 12 '23 at 07:08