Search code examples
seleniumselenium-chromedriverheadless

python selenium chrome - extensions with headless not working


I have:

  • selenium v4
  • chrome v77
  • chromedriver v77

Do chrome extensions work in headless mode?

This thread from 2016: https://bugs.chromium.org/p/chromium/issues/detail?id=604324

And this one from 2017: https://bugs.chromium.org/p/chromedriver/issues/detail?id=2071

These two articles suggest chromedrive does not support headless mode with extensions? Is this still true? Firefox does support headless with extensions ......

Please note I do not want to assign a profile with: ops.add_argument('user-data-dir=path_to_profile'). This should allow the solution. But I need the solution to work in threads ... without duplicating and assigning profiles manually to each thread.

Here is my code:

import selenium
from selenium.webdriver.chrome.options import Options as options
from selenium.webdriver.chrome.service import Service
#///////////////// Init binary & driver
new_binary_path = 'D:/path/Selenium_Test/chrome/chrome_exe/installed _32/chrome.exe'
new_driver_path = 'D:/path/Selenium_Test/chrome/chromedriver77_32.exe'
new_addon_path = r'D:\path\Selenium_Test\chrome\chrome_exe\uBlock.crx'

#///////////////// Init headless
serv = Service(new_driver_path)
ops = options()
ops.binary_location = new_binary_path
ops.add_argument("--headless")
#ops.headless = True (TRIED THIS -- doesnt work)
ops.add_extension(new_addon_path)
#ops.add_argument("--load-extension="+new_addon_path) (TRIED THIS -- doesnt work)

#///////////////// Init Browser
print('establishing 1st browser...')
browser1 = selenium.webdriver.Chrome(service=serv, options=ops)
browser1.set_page_load_timeout(int(load_page_timeout))

stacktrace:

selenium.common.exceptions.WebDriverException: Message: unknown error: failed to wait for extension background page to load: chrome-extension://cjpalhdlnbpafiamejdnhcphjbkeiagm/background.html
from unknown error: page could not be found: chrome-extension://cjpalhdlnbpafiamejdnhcphjbkeiagm/background.html
Stacktrace:
Backtrace:
    Ordinal0 [0x003FEB13+1501971]
    Ordinal0 [0x0037F6D1+980689]
    Ordinal0 [0x0030765F+489055]
    Ordinal0 [0x002F84FA+427258]
    Ordinal0 [0x0029C0D0+49360]
    Ordinal0 [0x002B9800+169984]
    Ordinal0 [0x002B942D+169005]
    Ordinal0 [0x002B78EB+162027]
    Ordinal0 [0x002A0AC7+68295]
    Ordinal0 [0x002A1B40+72512]
    Ordinal0 [0x002A1AD9+72409]
    Ordinal0 [0x00398F37+1085239]
    GetHandleVerifier [0x0049D7ED+503293]
    GetHandleVerifier [0x0049D580+502672]
    GetHandleVerifier [0x004A46AC+531644]
    GetHandleVerifier [0x0049DFFA+505354]
    Ordinal0 [0x00390606+1050118]
    Ordinal0 [0x0039047F+1049727]
    Ordinal0 [0x0039AF9B+1093531]
    Ordinal0 [0x0039B103+1093891]
    Ordinal0 [0x0039A095+1089685]
    BaseThreadInitThunk [0x76806359+25]
    RtlGetAppContainerNamedObjectPath [0x77427B74+228]
    RtlGetAppContainerNamedObjectPath [0x77427B44+180]

Thanks,


Solution

  • I know this is a super old question but I was struggling with the same issue until recently (2023) they upgraded the headless chrome which now allows headless to work with extensions. See webpage for more details (https://developer.chrome.com/articles/new-headless/)

    Just replace the headless option above with the below.

    ops.add_argument('--headless=new')

    According to the website if you just use --headless is still uses the old version and you have to explicitly point it to the new version to work.