gpt4 book ai didi

selenium - 为什么在Docker中运行ChromeDriver 2.31无法启动?

转载 作者:行者123 更新时间:2023-12-02 19:35:27 25 4
gpt4 key购买 nike

我有一个基于ubuntu:xenial的Docker容器。当我尝试使用selenium来运行Chrome时,网络驱动程序会不断加载

>>> from selenium import webdriver
>>> from selenium.webdriver.chrome.options import Options
>>> chrome_options = Options()
>>> chrome_options.add_argument("--headless")
>>> path_to_chromedriver = '/usr/local/bin/chromedriver'
>>> browser = webdriver.Chrome(path_to_chromedriver, chrome_options=chrome_options)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 140, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 229, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Linux 4.10.0-32-generic x86_64)

到目前为止的步骤

解决崩溃
>>> from selenium import webdriver
>>> browser = webdriver.Chrome()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
self.service.start()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/service.py", line 96, in start
self.assert_process_still_running()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/service.py", line 109, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127

当我尝试找到chromedriver版本时
chromedriver --version

我收到一个错误,即找不到共享库libnss3。安装 libnss3后,它可以工作。

所以线
RUN apt-get install libnss3

应该添加到Dockerfile中。

修复“找不到Chrome二进制文件”

安装Chrome。仅通过pip安装 chromedriver_installer是不够的。
RUN apt-get install wget
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install

最佳答案

当标准图像已经可用时,您正在尝试创建自定义图像。您应该使用selenium/chrome-standalone

docker run -d -p 4444:4444 selenium/standalone-chrom

然后在您的代码中启动RemoteWebdriver
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote(desired_capabilities=DesiredCapabilities.CHROME)

关于selenium - 为什么在Docker中运行ChromeDriver 2.31无法启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45793360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com