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

Selenium Python使用ActionChains时报元素不可交互错误如何解决?

问题1:PyCharm 未识别ActionChains的解决

你已经导入了ActionChains类,但实例化时调用的是webdriver.ActionChains(driver),IDE会判定你导入的类未被使用,改为直接用导入的类实例化即可:

# 原有导入保留
from selenium.webdriver.common.action_chains import ActionChains
# 实例化写法修改为
action = ActionChains(driver)

问题2:ElementNotInteractableException 报错解决

报错提示目标元素没有尺寸和位置,核心原因是你定位到的是页面中隐藏的搜索按钮:
谷歌首页匹配//input[@class='gNO89b']的元素共有2个,第一个默认处于隐藏状态,没有宽高和坐标信息,ActionChains无法对不可见元素执行操作。
另外你的代码缺少time库的导入,调用time.sleep会触发未定义报错。

修正后可运行的完整代码

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

driver = webdriver.Chrome()
driver.get("https://www.google.com/")
# 保留你原有的切换地区自定义方法
ship2usa(driver)
time.sleep(2)

# 显式等待可见且可点击的目标按钮
wait = WebDriverWait(driver, 10)
button = wait.until(EC.element_to_be_clickable(
    (By.XPATH, "//input[@class='gNO89b' and @value='Google Search']")
))

# 执行ActionChains操作
action = ActionChains(driver)
action.move_to_element(button).click().perform()

额外验证方法

如果运行仍有问题,可先打印元素的可见状态确认:

print(button.is_displayed())

返回True才是符合操作要求的可见元素。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 08:39:02