使用Selenium滚动网页表格遇ElementNotInteractableException错误求助
解决Selenium滚动表格时ElementNotInteractableException错误
你遇到的ElementNotInteractableException是因为代码中定位的<a name="crossref">元素并非可接收键盘输入的交互容器,要滚动目标表格,需要定位到表格所在的带滚动条的父容器。
错误原因分析
目标表格位于Cross Reference and Model Information下方的滚动容器内,原代码选择的锚点元素不具备交互能力,无法响应send_keys(Keys.PAGE_DOWN)操作。
解决方案
以下两种方法均可实现表格滚动:
方法1:使用JavaScript直接滚动容器
通过定位表格的滚动容器,调用JS代码控制滚动,无需依赖键盘交互:
import os 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 webdriver_manager.chrome import ChromeDriverManager if __name__ == '__main__': WAIT = 1 print(f"Checking Browser driver...") os.environ['WDM_LOG'] = '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 = "https://www.appliancepartspros.com/ge-dryer-timer-knob-we1m654-ap3995088.html" driver.get(link) # 滚动到目标区域 target_header = waitWD.until(EC.presence_of_element_located((By.XPATH,'//h2[text()="Cross Reference and Model Information"]'))) driver.execute_script("arguments[0].scrollIntoView(true);", target_header) # 定位表格的滚动容器 scroll_container = waitWD.until(EC.presence_of_element_located((By.XPATH, '//div[@class="table-responsive"]'))) # 执行JS滚动(每次滚动300像素,循环5次) for _ in range(5): driver.execute_script("arguments[0].scrollTop += 300;", scroll_container) # 可选:添加等待,让滚动效果可见 # driver.implicitly_wait(WAIT)
方法2:激活容器后发送键盘指令
先确保滚动容器处于可交互状态,激活后再发送PAGE_DOWN:
import os 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 from webdriver_manager.chrome import ChromeDriverManager if __name__ == '__main__': WAIT = 1 print(f"Checking Browser driver...") os.environ['WDM_LOG'] = '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 = "https://www.appliancepartspros.com/ge-dryer-timer-knob-we1m654-ap3995088.html" driver.get(link) # 滚动到目标区域 target_header = waitWD.until(EC.presence_of_element_located((By.XPATH,'//h2[text()="Cross Reference and Model Information"]'))) driver.execute_script("arguments[0].scrollIntoView(true);", target_header) # 定位表格的滚动容器并等待可交互 scroll_container = waitWD.until(EC.element_to_be_clickable((By.XPATH, '//div[@class="table-responsive"]'))) # 点击激活容器 scroll_container.click() # 发送PAGE_DOWN指令 for _ in range(5): scroll_container.send_keys(Keys.PAGE_DOWN) # 可选:添加等待 # driver.implicitly_wait(WAIT)
说明
- 两种方法均定位到表格的实际滚动容器(
table-responsive类的div),这是页面中真正承载表格并提供滚动功能的元素。 - 方法1的JS滚动更稳定,不受页面焦点影响;方法2模拟用户手动滚动,更贴近真实操作。
内容的提问来源于stack exchange,提问作者Rapid1898
相关产品推荐
相关产品推荐

