Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'ChromeOptions' object has no attribute 'headless'

I am using Selenium with 'undetected_chrome.' However, when I update to the latest version of Selenium, which is 4.14.0, and use it with 'undetected_chrome,' I encounter the following error: 'AttributeError: 'Options' object has no attribute 'headless'.

#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()

Error:

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'

It will work if we use driver = webdriver.Chrome(), or if we downgrade the Selenium version to 4.12.0. There seems to be some issue with the latest Selenium version.

Solution

In Selenium versions 4.13.0 and later, it is necessary to explicitly provide the 'headless' option in your Chrome or browser configuration. Unlike in previous versions where it defaulted to 'False,' starting from 4.13.0, you must specify this option to use in normal mode.

from selenium import webdriver
from undetected_chromedriver import Chrome

# We have to provide headless option to make it work.
options = webdriver.ChromeOptions()
options.headless=False
driver=Chrome(options=options)
like image 665
Mohit Kumar Avatar asked Mar 23 '26 01:03

Mohit Kumar


1 Answers

Yes, this problem is due to the change in selenium 4.13.0

This link says

"* remove deprecated headless methods"

https://github.com/SeleniumHQ/selenium/blob/9a6947ea4fd685b5f997415565bc5dc875a71321/py/CHANGES#L6

Please go through this link for complete discussion and workarounds:

https://github.com/ultrafunkamsterdam/undetected-chromedriver/issues/1584

As you said:

it will work if we use driver = webdriver.Chrome(), or if we downgrade the Selenium version to 4.12.0

like image 76
Talha Tayyab Avatar answered Mar 25 '26 14:03

Talha Tayyab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!