我尝试使用以下代码在网站上选择“Psychatrie”选项:
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
print(f"Checking Browser driver...")
options = Options()
options.add_argument("start-maximized")
options.add_argument('--log-level=3')
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()
link = "https://asu.kvs-sachsen.de/arztsuche/pages/search.jsf"
driver = webdriver.Chrome (service=srv, options=options)
waitWD = WebDriverWait (driver, 10)
driver.get (link)
waitWD.until(EC.presence_of_element_located((By.XPATH,'//div[@id="searchForm:specialismDetail:selectWindow"]'))).send_keys("Psychatrie")
但我只收到这个错误:
(selenium) C:\DEV\Fiverr2024\ORDER\schlosswaechter>python temp1.py
Checking Browser driver...
Traceback (most recent call last):
File "C:\DEV\Fiverr2024\ORDER\schlosswaechter\temp1.py", line 23, in <module>
waitWD.until(EC.presence_of_element_located((By.XPATH,'//div[@id="searchForm:specialismDetail:selectWindow"]'))).send_keys("Psychatrie")
File "C:\DEV\.venv\selenium\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 231, in send_keys
self._execute(
File "C:\DEV\.venv\selenium\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 395, in _execute
return self._parent.execute(command, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\DEV\.venv\selenium\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
self.error_handler.check_response(response)
File "C:\DEV\.venv\selenium\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=124.0.6367.202)
Stacktrace:
GetHandleVerifier [0x00007FF7E2331522+60802]
(No symbol) [0x00007FF7E22AAC22]
(No symbol) [0x00007FF7E2167B13]
(No symbol) [0x00007FF7E21B09F7]
(No symbol) [0x00007FF7E21AEB1A]
(No symbol) [0x00007FF7E21DAB7A]
(No symbol) [0x00007FF7E21AA7C6]
(No symbol) [0x00007FF7E21DAD90]
(No symbol) [0x00007FF7E21FA224]
(No symbol) [0x00007FF7E21DA923]
(No symbol) [0x00007FF7E21A8FEC]
(No symbol) [0x00007FF7E21A9C21]
GetHandleVerifier [0x00007FF7E26341BD+3217949]
GetHandleVerifier [0x00007FF7E2676157+3488183]
GetHandleVerifier [0x00007FF7E266F0DF+3459391]
GetHandleVerifier [0x00007FF7E23EB8E6+823622]
(No symbol) [0x00007FF7E22B5FBF]
(No symbol) [0x00007FF7E22B0EE4]
(No symbol) [0x00007FF7E22B1072]
(No symbol) [0x00007FF7E22A18C4]
BaseThreadInitThunk [0x00007FF8F4D0257D+29]
RtlUserThreadStart [0x00007FF8F676AA48+40]
如何从页面上的选择框中选择该值?
您打错了单词“Psychatrie”。应该是“精神病学”。
此外,我还修改了代码,以便您首先单击下拉菜单,然后从选项中搜索确切的字符串。最后,通过执行 javascript,您可以单击该项目。
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
print(f"Checking Browser driver...")
options = Options()
options.add_argument("start-maximized")
options.add_argument('--log-level=3')
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()
link = "https://asu.kvs-sachsen.de/arztsuche/pages/search.jsf"
driver = webdriver.Chrome(service=srv, options=options)
waitWD = WebDriverWait(driver, 10)
driver.get(link)
# First click on the drop-down menu
waitWD.until(EC.presence_of_element_located((By.XPATH, '//div[@id="searchForm:specialismDetail:selectWindow"]'))).click()
# Then search for the exact string
element = waitWD.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Psychiatrie']")))
driver.execute_script("arguments[0].click();", element)