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

Selenium 4.8.0 Python中headless属性弃用警告及修改方案咨询

关于Selenium无头模式DeprecationWarning的解释与修复

警告含义

这个警告明确表示:你当前使用的options.headless = True属性已被官方标记为弃用,后续版本可能会彻底移除该属性。官方不再维护这种设置无头模式的方式,转而推荐通过Chrome命令行参数来启用,提供了两个选项:

  • --headless:对应旧版无头模式,基于Chrome旧渲染引擎,兼容性较好但功能有限
  • --headless=new:对应新版无头模式,功能更接近正常Chrome浏览器,支持更多现代网页特性(如JavaScript API、扩展等)

修改方案

只需将代码中options.headless = True这一行替换为以下两种方式之一即可:

方式一:使用旧版无头模式参数

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

options = Options()
# 替换原有的options.headless = True
options.add_argument('--headless')
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get('https://www.google.com/')
driver.quit()

方式二:使用新版无头模式参数

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

options = Options()
# 替换原有的options.headless = True
options.add_argument('--headless=new')
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get('https://www.google.com/')
driver.quit()

如果业务需要更贴近正常浏览器的行为,推荐使用--headless=new;若遇到兼容性问题,可回退到--headless参数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 20:11:46