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

Selenium操作谷歌翻译输入框报错ElementNotInteractableException求助

谷歌翻译输入框无法输入文本的问题排查与解决

问题场景

尝试用Selenium自动化操作谷歌翻译网站,在输入框中输入文本时触发ElementNotInteractableException错误,核心代码及报错信息如下:

原代码

import time
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

if __name__ == '__main__':
      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()
      driver = webdriver.Chrome (service=srv, options=options)    
      waitWD = WebDriverWait (driver, 10)
      firstRun = True

      driver.get ("https://translate.google.com/")     
      time.sleep(3) 
      waitWD.until(EC.element_to_be_clickable((By.XPATH, '//button[@aria-label="Alle akzeptieren"]'))).click()   
      waitWD.until(EC.element_to_be_clickable((By.XPATH, '//c-wiz[@jsdata="deferred-c2"]'))).click()   
      waitWD.until(EC.element_to_be_clickable((By.XPATH, '//c-wiz[@jsdata="deferred-c2"]'))).send_keys("This is some test!")

报错信息

(openAIALL) C:\DEV\Fiverr\ORDER\robalf\SOLtranslateTXT>python test1.py
Traceback (most recent call last):
  File "C:\DEV\Fiverr\ORDER\robalf\SOLtranslateTXT\test1.py", line 28, in <module>
    waitWD.until(EC.element_to_be_clickable((By.XPATH, '//c-wiz[@jsdata="deferred-c2"]'))).send_keys("This is some test!")
  File "C:\DEV\.venv\openAIALL\lib\site-packages\selenium\webdriver\remote\webelement.py", line 231, in send_keys
    self._execute(
  File "C:\DEV\.venv\openAIALL\lib\site-packages\selenium\webdriver\remote\webelement.py", line 395, in _execute
    return self._parent.execute(command, params)
  File "C:\DEV\.venv\openAIALL\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "C:\DEV\.venv\openAIALL\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=120.0.6099.200)

原因分析

你定位的//c-wiz[@jsdata="deferred-c2"]是谷歌翻译输入区域的外层容器元素,并非实际接收用户输入的<textarea>或<input>控件。这类容器元素虽然可以被点击,但不具备接收文本输入的交互能力,直接调用send_keys()必然触发ElementNotInteractableException。

解决方案

  1. 定位实际输入控件:通过浏览器开发者工具(F12)查看谷歌翻译输入框的真实DOM结构,实际可输入的元素通常是带有aria-label="Quelltext"(德语环境下的“源文本”)的<textarea>标签。
  2. 优化等待逻辑:移除不必要的time.sleep(3),改用Selenium的显式等待确保元素完全可交互后再执行操作。

修正后的代码

import time
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

if __name__ == '__main__':
    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()
    driver = webdriver.Chrome(service=srv, options=options)    
    waitWD = WebDriverWait(driver, 10)

    driver.get("https://translate.google.com/")     
    # 等待并点击同意按钮(德语环境)
    waitWD.until(EC.element_to_be_clickable((By.XPATH, '//button[@aria-label="Alle akzeptieren"]'))).click()   
    # 定位实际的输入文本框并等待可交互
    input_box = waitWD.until(EC.element_to_be_clickable((By.XPATH, '//textarea[@aria-label="Quelltext"]')))
    # 输入测试文本
    input_box.send_keys("This is some test!")
    
    # 可选:停留查看结果后关闭浏览器
    # time.sleep(5)
    # driver.quit()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 22:08:13