pythongoogle-chromeselenium-webdriverselenium-chromedriverundetected-chromedriver

Undetected Chromedriver, AttributeError: 'ChromeOptions' object has no attribute 'headless'


I'm getting an error when I run a Python Selenium script to open a webpage. I have tried uninstalling and reinstalling selenium, chromeautoinstaller, and undetected chromedriver. I also tried adding option.add_argument('--headless'). None of these were successful and the error remained the same.

Here is my code:

    def driverInit():
        option = uc.ChromeOptions()
        option.add_argument("--log-level=3")
    
        prefs = {"credentials_enable_service": False,
                 "profile.password_manager_enabled": False,
                 "profile.default_content_setting_values.notifications": 2
                 }
        option.add_experimental_option("prefs", prefs)
        driverr = uc.Chrome(options=option)
        return driverr
    
    def driverInitBuffMarket():
        option = uc.ChromeOptions()
        option.add_argument(
             rf'--user-data-dir=C:\Users\{os.getlogin()}\AppData\Local\Google\ChromeBuff\User Data')  # e.g. C:\Users\You\AppData\Local\Google\Chrome\User Data
        option.add_argument(r'--profile-directory=Default')
        driverr = uc.Chrome(options=option)
        return driver

The error occurs in the second-to-last line, driverr = uc.Chrome(options=option)

Here is the error:

Traceback (most recent call last):
  File "C:\Users\kumpd\OneDrive\Desktop\All Market Bots\BuffMarket Purchase Bot Testing\main.py", line 266, in <module>
    start_buy_monitoring()
  File "C:\Users\kumpd\OneDrive\Desktop\All Market Bots\BuffMarket Purchase Bot Testing\main.py", line 207, in start_buy_monitoring
    driverBuffMarket = driverInitBuffMarket()
                       ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kumpd\OneDrive\Desktop\All Market Bots\BuffMarket Purchase Bot Testing\main.py", line 42, in driverInitBuffMarket
    driverr = uc.Chrome(options=option)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kumpd\AppData\Local\Programs\Python\Python311\Lib\site-packages\undetected_chromedriver\__init__.py", line 398, in __init__
    if headless or options.headless:
                   ^^^^^^^^^^^^^^^^
AttributeError: 'ChromeOptions' object has no attribute 'headless'

Any help is greatly appreciated!


Solution

  • As of today, undetected-chromedriver is still using options.headless in their code.

    That's a problem due to this change in selenium 4.13.0 that was just released:

    * remove deprecated headless methods
    

    Here are some alternatives:

    from seleniumbase import Driver
    import time
    
    driver = Driver(uc=True)
    driver.get("https://nowsecure.nl/#relax")
    time.sleep(6)
    driver.quit()
    

    Here's another, more advanced example with retries and clicks (using the SB() manager):

    from seleniumbase import SB
    
    with SB(uc=True) as sb:
        sb.driver.get("https://nowsecure.nl/#relax")
        sb.sleep(2)
        if not sb.is_text_visible("OH YEAH, you passed!", "h1"):
            sb.get_new_driver(undetectable=True)
            sb.driver.get("https://nowsecure.nl/#relax")
            sb.sleep(2)
        if not sb.is_text_visible("OH YEAH, you passed!", "h1"):
            if sb.is_element_visible('iframe[src*="challenge"]'):
                with sb.frame_switch('iframe[src*="challenge"]'):
                    sb.click("span.mark")
                    sb.sleep(4)
        sb.activate_demo_mode()
        sb.assert_text("OH YEAH, you passed!", "h1", timeout=3)
    

    (This is mainly what I wrote here: https://github.com/ultrafunkamsterdam/undetected-chromedriver/issues/1584#issuecomment-1737963363)