You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium操作德国医生查询站非原生下拉框失败求助

问题:自动化操作Fachrichtung自定义下拉框失败的解决方法

我尝试使用Selenium访问网站:https://gesund.bund.de/suchen/aerztinnen-und-aerzte,编写的代码如下:

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

我尝试选择页面中间的“Fachrichtung”(专科方向)下拉框选项,但操作失败。尝试了多种方法(如代码注释所示):使用Select类(提示该元素不是select元素)、执行点击脚本、直接输入文本,均无法生效。请问如何实现该元素的自动化操作?


解决方案

这个下拉框是自定义UI组件,不是原生HTML的<select>标签,所以不能用Select类操作。需要模拟真实用户的交互流程:点击下拉框展开选项列表,再点击目标选项。

修改后的核心代码(替换原下拉框操作部分):

# 等待下拉框容器可点击并点击,展开选项列表
dropdown_container = waitWD.until(EC.element_to_be_clickable((By.XPATH, '//div[@id="arztsuche-fachrichtung-list"]')))
dropdown_container.click()

# 等待目标选项"Hausarzt"可点击并选择
target_option = waitWD.until(EC.element_to_be_clickable((By.XPATH, '//li[contains(text(), "Hausarzt")]')))
target_option.click()

关键说明:

  1. 显式等待优先:用element_to_be_clickable代替presence_of_element_located,确保元素不仅存在还能交互,避免因元素未加载完成导致的操作失败。
  2. 模拟真实交互:先点击下拉框触发选项列表展开,再定位并点击目标选项,完全复刻用户操作逻辑。
  3. 避免硬延迟:尽量用WebDriverWait替代time.sleep,提升代码稳定性和执行效率。

完整修改后代码:

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)
  
  # 处理邮编输入
  zip_input = waitWD.until(EC.element_to_be_clickable((By.XPATH,'//input[@id="arztsuche__field_where"]')))
  zip_input.clear()
  zip_input.send_keys("13403")

  # 处理Fachrichtung下拉框
  # 点击下拉框展开选项
  dropdown_container = waitWD.until(EC.element_to_be_clickable((By.XPATH, '//div[@id="arztsuche-fachrichtung-list"]')))
  dropdown_container.click()
  # 点击目标选项
  target_option = waitWD.until(EC.element_to_be_clickable((By.XPATH, '//li[contains(text(), "Hausarzt")]')))
  target_option.click()

input("Press!")

内容的提问来源于stack exchange,提问作者Rapid1898

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 00:21:26