未知错误:无法等待扩展程序后台页面加载:chrome-extension错误,使用Selenium将扩展程序加载到Chrome Headless [英] unknown error: failed to wait for extension background page to load: chrome-extension error loading an extension to Chrome Headless using Selenium
问题描述
我尝试以无头模式通过硒运行chromedriver.
I try to run chromedriver via selenium in headless mode.
重要如果我消除以下代码行(但不是无头的话),则代码可以正常运行:
IMPORTANT The code runs perfectly fine if I eliminate the following code lines (but is not headless):
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
这是我尝试实现无头参数时遇到的错误:
This is the error I get when I try to implement the headless argument:
Traceback (most recent call last):
File "camel.py", line 83, in <module>
executable_path=executable_path)
File "/home/.local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/home/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: failed to wait for extension background page to load: chrome-extension://jkompbllimaoekaogchhkmkdogpkhojg/_generated_background_page.html
from unknown error: page could not be found: chrome-extension://jkompbllimaoekaogchhkmkdogpkhojg/_generated_background_page.html
这是第81、82和83行
This are the lines 81, 82 and 83
chrome_options.add_extension(extension_path)
driver = webdriver.Chrome(options=chrome_options,
executable_path=executable_path)
这是代码(crhomedriver执行部分):
This is the code (the crhomedriver execution parts):
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import presence_of_element_located
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
log_path = os.path.join(BASE_DIR, 'cronJobChromeDriver.log')
executable_path = os.path.join(BASE_DIR, 'chromedriver_linux64/chromedriver')
extension_path = os.path.join(
BASE_DIR, 'chromedriver_linux64/extension_2_8_9_0.crx')
print('executable_path', executable_path)
推荐答案
最底行是否, google-chrome-headless 不支持扩展.
The bottom line is, No, google-chrome-headless doesn't supports extensions.
在他的评论之一中,提到了 alexclarke@chromium.org :
In the one of his comment, alexclarke@chromium.org mentioned:
我知道很多人都想无头使用扩展,但是不幸的是,这是一个大项目,我们尚无计划/.问题在于Headless Chromium是一个内容嵌入程序,这意味着它无法访问其他内容嵌入程序(例如chrome)中的任何内容,不幸的是扩展是chrome功能.
I realize a lot of folks would like to use extensions with headless but unfortunately that's a large project which we have /no plans to do/. The problem is Headless Chromium is a content embedder which means it doesn't have access to anything from other content embedders such as chrome and unfortunately extensions are a chrome feature.
在另一个评论中,他进一步补充说,如果您通过
In another comment he further added, if you're using Selenium through DevTools you can build a proxy. Next you can filter URLs and modify headers via Network.setRequestInterception and Network.continueInterceptedRequest.
您可以在以下位置找到相关的详细讨论:
You can find a relevant detailed discussion in:
这篇关于未知错误:无法等待扩展程序后台页面加载:chrome-extension错误,使用Selenium将扩展程序加载到Chrome Headless的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!