Search code examples
pythonselenium-webdriverxpathwebdriverwebdriverwait

Selenium / select dropbox?


i try to use selenium with this site:

https://gesund.bund.de/suchen/aerztinnen-und-aerzte

with the following code:

import time
import os, sys

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from webdriver_manager.chrome import ChromeDriverManager
# from fake_useragent import UserAgent

if __name__ == '__main__':
  path = os.path.abspath(os.path.dirname(sys.argv[0]))

  options = Options()
  options.add_argument("start-maximized")
  options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
  options.add_experimental_option("excludeSwitches", ["enable-automation"])
  options.add_experimental_option('excludeSwitches', ['enable-logging'])
  options.add_experimental_option('useAutomationExtension', False)
  options.add_argument('--disable-blink-features=AutomationControlled')
  srv=Service(ChromeDriverManager().install())
  driver = webdriver.Chrome (service=srv, options=options)
  waitWD = WebDriverWait (driver, 10)

  link = f"https://gesund.bund.de/suchen/aerztinnen-und-aerzte"  
  driver.get (link)
  waitWD.until(EC.presence_of_element_located((By.XPATH,'//input[@id="arztsuche__field_where"]'))).clear()
  time.sleep(0.5)
  waitWD.until(EC.presence_of_element_located((By.XPATH,'//input[@id="arztsuche__field_where"]'))).send_keys("13403")
  time.sleep(0.5)

  # select = Select(driver.find_element(By.XPATH,'//div[@id="arztsuche-fachrichtung-list"]'))
  # driver.execute_script("arguments[0].click();", waitWD.until(EC.element_to_be_clickable((By.XPATH, '//div[@id="arztsuche-fachrichtung-list"]'))))    
  waitWD.until(EC.presence_of_element_located((By.XPATH,'//div[@id="arztsuche-fachrichtung-list"]'))).send_keys("Hausarzt")    

input("Press!")

I try to select an entry from the combobox "Fachrichtung" i the middle but it is not working. I tried several thing like you can see in the comment:

  # select = Select(driver.find_element(By.XPATH,'//div[@id="arztsuche-fachrichtung-list"]'))
  # driver.execute_script("arguments[0].click();", waitWD.until(EC.element_to_be_clickable((By.XPATH, '//div[@id="arztsuche-fachrichtung-list"]'))))    
  waitWD.until(EC.presence_of_element_located((By.XPATH,'//div[@id="arztsuche-fachrichtung-list"]'))).send_keys("Hausarzt") 

Tried to use select (but it tells me its no select-element). Tried to click on it - but it is not clickable. And tried to simple assign a text to it - but this is also not working.

How can i automate the element on this site?


Solution

  • It is an Auto Complete form,Select won't work here. You need to identify the element and then click on the element after search result appear on the autocomplete form.

    waitWD = WebDriverWait (driver, 10)
    
    link = "https://gesund.bund.de/suchen/aerztinnen-und-aerzte"  
    driver.get (link)
    searchstring="13403"
    waitWD.until(EC.element_to_be_clickable((By.XPATH,'//input[@id="arztsuche__field_where"]'))).clear()
    time.sleep(0.5)
    waitWD.until(EC.element_to_be_clickable((By.XPATH,'//input[@id="arztsuche__field_where"]'))).send_keys(searchstring)
    time.sleep(0.5)
    waitWD.until(EC.element_to_be_clickable((By.XPATH,"//span[text()='{}']".format(searchstring)))).click()
    

    Browser Snapshot:

    enter image description here

    UPDATE:

    waitWD = WebDriverWait (driver, 10)
    
    link = "https://gesund.bund.de/suchen/aerztinnen-und-aerzte"  
    driver.get (link)
    searchstring="13403"
    waitWD.until(EC.element_to_be_clickable((By.XPATH,'//input[@id="arztsuche__field_where"]'))).clear()
    time.sleep(0.5)
    waitWD.until(EC.element_to_be_clickable((By.XPATH,'//input[@id="arztsuche__field_where"]'))).send_keys(searchstring)
    time.sleep(0.5)
    waitWD.until(EC.element_to_be_clickable((By.XPATH,"//span[text()='{}']".format(searchstring)))).click()
    
    waitWD.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input#arztsuche-fachrichtung"))).click()
    secondField='Hausarzt'
    waitWD.until(EC.element_to_be_clickable((By.XPATH,"//span[text()='{}']".format(secondField)))).click()
    

    BrowserSnapshot

    enter image description here