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

Python Selenium输入文本报错:element not interactable问题求助

问题:Python Selenium输入文本时提示element not interactable

尝试用Python结合Selenium向输入框输入文本,却持续报错element not interactable,以下是相关信息及尝试过的操作:

尝试的代码1(Tab导航后输入)

webdriver.ActionChains(self.driver).send_keys(Keys.TAB).perform()
webdriver.ActionChains(self.driver).send_keys(Keys.TAB).perform()
time.sleep(15)
WebDriverWait(self.driver, 15).until(EC.presence_of_element_located((By.XPATH, "//span[contains(@class, 'input-container')]"))).send_keys("test")

尝试的代码2(点击元素后输入)

WebDriverWait( self.driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(@class,'input-container')]"))).click()
time.sleep(15)
WebDriverWait(self.driver, 15).until(EC.presence_of_element_located((By.XPATH, "//span[contains(@class, 'input-container')]"))).send_keys("test")

已添加强制等待确认点击到文本框,但仍收到相同错误。

完整堆栈跟踪信息

Traceback (most recent call last):
File "/Users/user1/Desktop/btm.py", line 124, in <module>
b.begin()
File "/Users/user1/Desktop/btm.py", line 80, in begin
WebDriverWait(self.driver, 15).until(EC.presence_of_element_located((By.XPATH, 
"//span[contains(@class, 'input-container')]"))).send_keys("test")
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site- 
packages/selenium/webdriver/remote/webelement.py", line 233, in send_keys
self._execute(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site- 
packages/selenium/webdriver/remote/webelement.py", line 410, in _execute
return self._parent.execute(command, params)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site- 
packages/selenium/webdriver/remote/webdriver.py", line 444, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site- 
packages/selenium/webdriver/remote/errorhandler.py", line 249, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not 
interactable
(Session info: chrome=108.0.5359.98)

输入元素的HTML结构

<span class="input-container span-to-div"><input aria-label="Blank 1 of 1" autocapitalize="off" autocomplete="new-password" autocorrect="off" class="fitb-input " id="fitbTesting_eebce337-d24b-4253-82cd-878f339bf49b" maxlength="50" name="fitbTesting_4bfacf11-846f-48c8-9821-f276b3ef43b8" spellcheck="false" value=""><span aria-label="Empty Blank 1 of 1" class="_visuallyHidden"></span></span>

界面截图

输入框界面截图


解决方案

核心问题:你一直定位的是<span class="input-container">元素,但真正能接收文本输入的是该span内部的<input>标签,span本身并非可输入元素,因此调用send_keys会触发element not interactable错误。

修改代码,直接定位内部的input元素即可,以下是几种可行方式:

方式1:通过input的class定位

WebDriverWait(self.driver, 15).until(EC.element_to_be_clickable((By.CLASS_NAME, "fitb-input"))).send_keys("test")

方式2:通过xpath关联父元素定位input

WebDriverWait(self.driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(@class, 'input-container')]/input"))).send_keys("test")

方式3:通过input的aria-label属性定位

WebDriverWait(self.driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//input[@aria-label='Blank 1 of 1']"))).send_keys("test")

额外注意:

  • 替换presence_of_element_located为element_to_be_clickable,前者仅确保元素存在,后者能保证元素处于可交互状态
  • 移除不必要的time.sleep,显式等待比强制等待更高效可靠

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 00:01:09