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

使用Python-Selenium填写数独触发element not interactable错误如何解决

问题原因分析
  • 你定位的目标元素是<div>标签,本身不属于原生可输入的表单元素,默认不支持send_keys输入操作,这是抛出element not interactable的核心原因。
  • 该数独网站的输入逻辑为:点击目标格子激活选中状态后,由页面全局键盘事件监听捕获输入的数字,再赋值到对应格子,不需要直接向格子元素输入内容。
  • 硬sleep的稳定性差,容易出现元素未完全渲染完成就执行操作的情况。
可行解决方案

使用WebDriverWait等待元素可点击后,先点击激活对应格子,再通过ActionChains模拟全局键盘输入即可。同时推荐适配Selenium 4+的标准元素定位写法,避免旧API弃用报错。

完整可运行代码示例

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get('https://sudokutable.com/')
wait = WebDriverWait(driver, 10)

# 等待目标格子可点击后定位
target_cell = wait.until(EC.element_to_be_clickable(
    (By.XPATH, '//*[@id="body_wrapper"]/main/section[2]/div[1]/div[2]/div[1]')
))
# 点击激活格子
target_cell.click()
# 模拟键盘输入数字1
ActionChains(driver).send_keys('1').perform()
可选替代方案

如果点击后输入仍然无效,可以直接通过执行JS修改格子的文本内容,同时触发对应的页面事件:

driver.execute_script("arguments[0].textContent = '1'; arguments[0].dispatchEvent(new Event('input'));", target_cell)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 14:06:03