使用python selenium将adblocker扩展与headless chrome驱动程序一起使用

2024-04-28 20:19:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用adblocker无头运行我的chrome驱动程序,两者分开运行不会出错,但添加这两个选项会导致以下错误:

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

据我所知,adblock扩展正在查找背景页,但没有找到它,因为它处于无头模式。(无头模式工作正常)

可测试代码(给出错误):

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)

我尝试过的事情:

·不同的广告阻滞剂。(可以提供列表)

·让代码在不同的地方等待。(正如错误所说,它无法等待 一些东西)

·无头,但没有adblocker。(比adblocker和显示窗口的GUI时慢)

·多次向谷歌提问。(没用…)

如果有人知道一个使用python selenium运行chrome浏览器headless的解决方案,我也想知道,谢谢。在


Tags: tofromforselenium错误extensionpageerror