简体   繁体   中英

Using an adblocker extension with the headless chrome driver using python selenium

I am trying to use an adblocker and running my chrome driver headlessly, doing both separately gives me no error but adding both options gives me the following error:

selenium.common.exceptions.WebDriverException: Message: unknown error: failed to wait for extension background page to load: chrome-extension://alplpnakfeabeiebipdmaenpmbgknjce/_generated_background_page.html from unknown error: page could not be found: chrome-extension://alplpnakfeabeiebipdmaenpmbgknjce/_generated_background_page.html

From what I could understand, the adblock extension is looking for the background page and isn't finding it as it is in headless mode. (it works fine without the headless mode)

Testable code (gives the error):

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
# adblocker crx file, downloaded from: https://chrome-extension-downloader.com/
chrome_options.add_extension("Adblocker-for-Chrome-NoAds_v3.2.0.1.crx")
chrome_options.add_argument("headless")

driver = webdriver.Chrome(options=chrome_options)

Things I have tried:

·Different adblocker. (can provide a list)

·Making the code wait at various place. (as the error said it failed to wait for something)

·Going headless but without an adblocker. (was slower than with an adblocker and the window's GUI showing)

·Asking google multiple times the question. (didn't work...)

If anyone knows a solution to run the chrome browser headless using python selenium while having an adblock extension, I would like to know it too, thanks.

Running selenium in headless mode with extensions is currently not possible and as it seems, Google isn't planning on supporting that any time soon. You can find more information here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM