我试图在运行高寒的Docker容器中使用Python和selenium运行chrome。它运行良好,直到有一天,当我实例化chrome时,它开始抛出以下错误。
Message: unknown error: Chrome failed to start: crashed.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/lib/chromium/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
代码
下面是创建此错误的代码:
from selenium import webdriver
def generate_plugin():
pluginfile = 'proxy_auth_plugin.zip'
# manifest_json, background_js same as https://stackoverflow.com/a/61764363/9809865
with zipfile.ZipFile(pluginfile, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
return pluginfile
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
plugin = generate_plugin()
chrome_options.add_extension(plugin)
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument("--remote-debugging-port=9222")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://google.com")
它生成的异常的完整跟踪。
全回溯
driver = webdriver.Chrome(chrome_options=chrome_options)
| File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
| desired_capabilities=desired_capabilities)
| File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
| self.start_session(capabilities, browser_profile)
| File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
| response = self.execute(Command.NEW_SESSION, parameters)
| File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
| self.error_handler.check_response(response)
| File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
| raise exception_class(message, screen, stacktrace)
Message: unknown error: Chrome failed to start: crashed.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/lib/chromium/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
我使用pyvirtualdisplay
来显示webdriver,但是即使我在代码中添加了--headless
选项,它也会引发相同的错误。
Dockerfile
这是我的Dockerfile:
FROM python:3.6-alpine
RUN apk add xvfb
# update apk repo
RUN echo "http://dl-4.alpinelinux.org/alpine/v3.7/main" >> /etc/apk/repositories && \
echo "http://dl-4.alpinelinux.org/alpine/v3.7/community" >> /etc/apk/repositories
# install chromedriver
RUN apk update
RUN apk add chromium=93.0.4577.82-r2 --repository=http://dl-cdn.alpinelinux.org/alpine/v3.15/community chromium-chromedriver=93.0.4577.82-r2
/usr/lib/chromium # ./chrome --version
Chromium 93.0.4577.82
/usr/lib/chromium # ./chromedriver --version
ChromeDriver 93.0.4577.82 (e3a25d9b9e2d0b728e045ec87c0aa4942aa46e4e-refs/branch-heads/4577@{#1237})
/usr/lib/chromium #
原版
我最初使用的是RUN apk add chromium=86.0.4240.111-r0 --repository=http://dl-cdn.alpinelinux.org/alpine/v3.13/community chromium-chromedriver=86.0.4240.111-r0
在它开始抛出异常之前,它一直运行良好。因此,我去了我的码头集装箱,并试图运行Chrome手动。它抛出了这个错误
Error relocating ./chrome: hb_subset_input_set_drop_hints: symbol not found
Error relocating ./chrome: hb_subset_input_set_retain_gids: symbol not found
Error relocating ./chrome: hb_subset: symbol not found
一旦我将Chrome版本升级到93.0.4577.82,我可以手动启动Chrome和Chromedriver,但是运行代码会产生相同的回溯。
OS和Docker版本
我使用Macbook Pro M1 (2020)和MacOS 12.1
最新版本: 4.3.2 (72729)
我试过的东西
我已经看过StackOverflow和其他论坛上提出的几乎所有其他问题,但到目前为止,没有一条建议对我有效。
更新:
传递--headless
参数使其运行。这使我假设这个问题与使用pyvirtualdisplay
有关。
更新2
我错过了添加我正在创建一个扩展压缩文件添加到Chrome的动态。
现在的情况如下:
selenium.common.exceptions.WebDriverException: Message: Message: unknown error: failed to wait for extension background page to load: chrome-extension://ilkejjjhcpjkcecgdkkdfaocminnclpo/_generated_background_page.html
。--headless
参数并禁用扩展,可以完全正确地工作--headless
参数并启用扩展,它将引发与第2点相同的异常。发布于 2022-03-17 12:02:12
我在M1和官方硒码头图像上也有过同样的问题。
但是有一些关于如何修复它的段落:
有关在Mac M1或Raspberry Pi等平台上运行的试验性坞容器映像,请参阅托管在seleniumhq-社区/码头-自带武器上的社区驱动存储库。这些映像是为三种不同的体系结构构建的: linux/arm64 (aarch64)、linux/arm/v7 (armhf)和linux/ and 64。 此外,这些实验容器图像被发布在自带武器码头集线器注册表上。 有关这些图像的更多信息,请参见发布#1076。 如果您正在使用Intel或AMD64体系结构,我们建议使用这个存储库中的容器映像(SeleniumHQ/docker)而不是试验性映像。
发布于 2022-05-06 18:15:01
在将chromedriver版本从AWS Lambda (Amazon 2)的3.6升级到86和Python运行时后,我得到了这个错误。我玩了几个小时的鼹鼠与铬/铬驱动器启动问题。
最后,我发现这是积极维护的python+selenium+docker的最小实现。https://github.com/umihico/docker-selenium-lambda/,他们的Dockerfile和test.py chrome_options中的设置成功了。
https://stackoverflow.com/questions/70575636
复制相似问题