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

使用Python Selenium实现谷歌搜索时遇ElementNotInteractableException问题

解决Selenium谷歌搜索的Cookie弹窗与ElementNotInteractableException错误

问题描述

执行Python Selenium谷歌搜索脚本时,出现ElementNotInteractableException错误,原因是Chrome启动后弹出的"Before you continue with Google" Cookie授权弹窗遮挡了搜索输入框,导致无法定位并操作元素。

原代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--disable-notifications")

# Create an instance of the Chrome driver
driver = webdriver.Chrome(options=chrome_options)

# Navigate to Google
driver.get("https://www.google.com")

# Find the search input element and enter the query
search_input = driver.find_element("name", "q")
search_input.send_keys('BBC News')

driver.quit()

错误信息

/Users/johna/PycharmProjects/pythonProject/venv/bin/python /Users/johna/PycharmProjects/pythonProject/ChatgptTest.py 
Traceback (most recent call last):
File "/Users/johna/PycharmProjects/pythonProject/ChatgptTest.py", line 12, in <module>
search_input.send_keys('BBC News')
  File "/Users/johna/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webelement.py", line 231, in send_keys
self._execute(
File "/Users/johna/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webelement.py", line 403, in _execute
return self._parent.execute(command, params)
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/johna/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "/Users/johna/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=113.0.5672.92)
Stacktrace:
0   chromedriver    0x0000000103b54ae8 chromedriver + 4958952
1   chromedriver    0x0000000103b4c7b3 chromedriver + 4925363
2   chromedriver    0x000000010370e051 chromedriver + 475217
3   chromedriver    0x0000000103749c2c chromedriver + 719916
4   chromedriver    0x00000001037490f6 chromedriver + 717046
5   chromedriver    0x0000000103777c12 chromedriver + 908306
6   chromedriver    0x00000001037438f1 chromedriver + 694513
7   chromedriver    0x0000000103777dce chromedriver + 908750
8   chromedriver    0x000000010379325b chromedriver + 1020507
9   chromedriver    0x00000001037779e3 chromedriver + 907747
10  chromedriver    0x0000000103741aef chromedriver + 686831
11  chromedriver    0x000000010374300e chromedriver + 692238
12  chromedriver    0x0000000103b109ad chromedriver + 4680109
13  chromedriver    0x0000000103b15c12 chromedriver + 4701202
14  chromedriver    0x0000000103b1c79f chromedriver + 4728735
15  chromedriver    0x0000000103b16b7a chromedriver + 4705146
16  chromedriver    0x0000000103ae927e chromedriver + 4518526
17  chromedriver    0x0000000103b36908 chromedriver + 4835592
18  chromedriver    0x0000000103b36a87 chromedriver + 4835975
19  chromedriver    0x0000000103b4600f chromedriver + 4898831
20  libsystem_pthread.dylib    0x00007fff728b7109 _pthread_start + 148
21  libsystem_pthread.dylib    0x00007fff728b2b8b thread_start + 15

Process finished with exit code 1

预期行为:打开谷歌并搜索‘BBC News’后结束运行。

解决方案

方法1:自动点击Cookie授权按钮

通过显式等待定位弹窗中的"接受所有Cookie"按钮,点击后再操作搜索框。

修改后的代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

chrome_options = Options()
chrome_options.add_argument("--disable-notifications")

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.google.com")

# 等待Cookie弹窗加载并点击接受按钮
try:
    accept_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Accept all')]"))
    )
    accept_button.click()
except:
    # 若弹窗未出现,直接跳过
    pass

# 操作搜索框
search_input = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.NAME, "q"))
)
search_input.send_keys('BBC News')

driver.quit()

说明:使用WebDriverWait等待元素加载,避免因页面未完全渲染导致的定位失败;添加异常捕获处理弹窗未出现的情况。

方法2:通过Chrome参数预设Cookie偏好

直接在启动Chrome时设置默认同意Cookie,跳过弹窗:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
# 添加Cookie预设参数
chrome_options.add_argument("--disable-features=PrivacySandboxSettings4")
chrome_options.add_experimental_option("prefs", {
    "profile.default_content_setting_values.cookies": 1,
    "profile.cookie_controls_mode": 0
})

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.google.com")

search_input = driver.find_element("name", "q")
search_input.send_keys('BBC News')

driver.quit()

说明:通过prefs设置Cookie权限为允许,profile.cookie_controls_mode=0表示关闭Cookie控制弹窗。

方法3:直接访问谷歌搜索结果页

绕过谷歌首页,直接使用带搜索参数的URL:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--disable-notifications")

driver = webdriver.Chrome(options=chrome_options)
# 直接访问搜索结果页
driver.get("https://www.google.com/search?q=BBC+News")

driver.quit()

说明:此方法完全跳过首页弹窗,直接加载搜索结果,适合仅需执行搜索的场景。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 01:42:05