使用undetected chromedriver报错:ChromeOptions无headless属性
解决
AttributeError: 'ChromeOptions' object has no attribute 'headless'问题 问题分析
你的报错源于undetected-chromedriver内部代码尝试访问options.headless属性,但当前ChromeOptions对象不存在该属性。结合你的代码,核心问题有两点:
- 创建了
ChromeOptions实例但未传递给uc.Chrome()构造函数; - 新旧版本的selenium/undetected-chromedriver对headless模式的设置方式存在兼容性差异。
解决方案
方案1:正确传递配置并使用兼容的headless参数
修改代码,将自定义的options传入驱动,并使用Chrome推荐的新版headless参数(适配Chrome 112+版本):
if __name__ == '__main__': import time from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys import undetected_chromedriver as uc # 初始化undetected-chromedriver的ChromeOptions options = uc.ChromeOptions() # 新版Chrome使用--headless=new,旧版可保留--headless options.add_argument('--headless=new') # 将配置传入驱动构造函数 driver = uc.Chrome(options=options) driver.get('https://google.com')
方案2:版本兼容调整
如果方案1无效,检查并升级依赖版本:
- 升级undetected-chromedriver到最新版:
pip install --upgrade undetected-chromedriver - 确保selenium版本为4.x及以上(与最新undetected-chromedriver兼容):
pip install --upgrade selenium
补充说明
旧版本的undetected-chromedriver会直接读取options.headless属性,但新版本selenium的ChromeOptions已不再暴露该属性,转而通过命令行参数控制headless模式。因此必须确保:
- 始终将自定义的
options传递给uc.Chrome(); - 使用对应Chrome版本支持的headless参数。
内容的提问来源于stack exchange,提问作者Adam Ray
相关产品推荐
相关产品推荐

