Undetected Chromedriver报错:'ChromeOptions'对象无'headless'属性
解决Undetected Chromedriver的AttributeError问题
这个错误的核心原因是:undetected-chromedriver(以下简称uc)的ChromeOptions对象不再支持直接设置headless属性,你大概率在代码里写了类似option.headless = True的语句,这和uc的API不兼容。
解决方案
直接通过命令行参数添加无头模式,不要用属性赋值的方式:
方式1:结合标准Selenium Options
from selenium.webdriver.chrome.options import Options import undetected_chromedriver as uc option = Options() # 启用新版无头模式(适配Chrome 112+) option.add_argument('--headless=new') # 可选:禁用GPU,避免部分环境报错 option.add_argument('--disable-gpu') driverr = uc.Chrome(options=option)
方式2:使用uc自带的ChromeOptions
import undetected_chromedriver as uc option = uc.ChromeOptions() option.add_argument('--headless=new') driverr = uc.Chrome(options=option)
补充说明
uc的API更新后,移除了headless属性的直接设置入口,必须通过add_argument传递浏览器启动参数。你之前尝试的option.add_argument('--headless')属于旧版参数,新版Chrome推荐用--headless=new来获得更接近正常浏览器的行为。
内容的提问来源于stack exchange,提问作者Dallin Kump
相关产品推荐
相关产品推荐

