Selenium选择下拉框选项遇ElementNotInteractableException的解决方法
解决Selenium操作ASU KVS萨克森网站下拉框的ElementNotInteractableException错误
你遇到的ElementNotInteractableException是因为代码定位的外层div元素并非可交互的输入控件。该页面的下拉框是PrimeFaces的SelectOneMenu组件,需要定位到组件内部的实际交互元素才能操作。
解决方案
针对该页面的下拉框,有两种可靠的操作方式:
方式一:展开下拉框后搜索选择
通过点击下拉框展开选项列表,输入关键词过滤后点击目标选项:
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) # 点击下拉框组件展开选项列表 dropdown = waitWD.until(EC.element_to_be_clickable((By.ID, "searchForm:specialismDetail"))) dropdown.click() # 定位下拉框内的搜索输入框,输入关键词 search_box = waitWD.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".ui-selectonemenu-filter"))) search_box.send_keys("Psychatrie") # 等待目标选项出现并点击 target_option = waitWD.until(EC.element_to_be_clickable((By.XPATH, '//li[text()="Psychatrie"]'))) target_option.click()
方式二:直接定位内部输入框
直接定位到下拉框组件内的输入控件,输入关键词后回车确认:
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.common.keys import Keys 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) # 定位下拉框内部的输入控件 input_field = waitWD.until(EC.element_to_be_clickable((By.XPATH, '//div[@id="searchForm:specialismDetail:selectWindow"]/input'))) # 输入关键词并回车选择 input_field.send_keys("Psychatrie" + Keys.ENTER)
错误原因说明
原代码中定位的//div[@id="searchForm:specialismDetail:selectWindow"]是下拉框的外层容器div,该元素不具备输入交互能力,真正接收输入的是其内部嵌套的input标签,因此直接对div调用send_keys会触发元素不可交互的异常。
内容的提问来源于stack exchange,提问作者Rapid1898
相关产品推荐
相关产品推荐

