Selenium 4.14.0与undetected_chromedriver搭配出现AttributeError问题及解决
解决Selenium 4.14.0 结合 undetected-chromedriver 3.5.0 出现的 AttributeError 问题
问题说明
将Selenium版本更新至4.14.0后,使用undetected_chromedriver.Chrome()初始化驱动时触发AttributeError: 'ChromeOptions' object has no attribute 'headless'错误。但直接使用webdriver.Chrome()或降级Selenium至4.12.0时可正常运行,此问题源于Selenium新版本的API兼容性变更。
错误重现代码
# 安装依赖 # pip install selenium==4.14.0 # pip install undetected-chromedriver==3.5.0 from selenium import webdriver from undetected_chromedriver import Chrome # driver=webdriver.Chrome() # 此方式可正常运行 driver=Chrome() driver.get('https://stackoverflow.com/') input('Enter to close window') driver.quit()
报错信息
Traceback (most recent call last): File "d:\Python\browser.py", line 8, in <module> driver=Chrome() ^^^^^^^^ File "D:\Python\venv\Lib\site-packages\undetected_chromedriver\__init__.py", line 375, in __init__ if headless or options.headless: ^^^^^^^^^^^^^^^^ AttributeError: 'ChromeOptions' object has no attribute 'headless'
解决方案
Selenium 4.13.0及后续版本中,ChromeOptions不再默认设置headless属性,需显式配置该选项才能与undetected-chromedriver兼容。修改后的代码如下:
from selenium import webdriver from undetected_chromedriver import Chrome # 显式配置headless选项 options = webdriver.ChromeOptions() options.headless = False driver = Chrome(options=options) # 后续操作示例 driver.get('https://stackoverflow.com/') input('Enter to close window') driver.quit()
内容的提问来源于stack exchange,提问作者Mohit Kumar
相关产品推荐
相关产品推荐

