Python Docker Selenium:Selenium.common.exceptions.WebDriverException:消息:服务chromedriver意外退出。状态代码为:127

Python Docker Selenium:Selenium.common.exceptions.WebDriverException:消息:服务chromedriver意外退出。状态代码为:127,python,selenium,docker,selenium-webdriver,selenium-chromedriver,Python,Selenium,Docker,Selenium Webdriver,Selenium Chromedriver,我在python项目中使用selenium的chromedriver 我正在成功构建Dockerfile: FROM ubuntu:17.04 FROM selenium/standalone-chrome FROM python:3.6 RUN apt update RUN apt-get install -y libnss3 libgconf-2-4 ADD ./requirements.txt /tmp/requirements.txt RUN python -m pip install

我在python项目中使用selenium的chromedriver

我正在成功构建Dockerfile:

FROM ubuntu:17.04
FROM selenium/standalone-chrome
FROM python:3.6
RUN apt update
RUN apt-get install -y libnss3 libgconf-2-4
ADD ./requirements.txt /tmp/requirements.txt
RUN python -m pip install -r /tmp/requirements.txt
ADD . /opt/example1/
# rights?
RUN chmod +x /opt/example1/assets/chromedriver
WORKDIR /opt/example1
CMD ["python","-u","program.py"]
但是,当我运行docker容器时,出现以下错误:

回溯(最近一次调用last):文件“program.py”,第8行,在 MdCrawler(MD_START_URL,“MobileDe”).START()文件“/opt/example1/mobile_de_crawler.py”,第17行,在init self.web\u driver\u chrome=webdriver.chrome(可执行路径=chrome\u driver\u path)文件 “/usr/local/lib/python3.6/site packages/selenium/webdriver/chrome/webdriver.py”, 第73行,在init self.service.start()文件“/usr/local/lib/python3.6/site packages/selenium/webdriver/common/service.py”, 第98行,开始 self.assert\u process\u仍然在运行()文件“/usr/local/lib/python3.6/site packages/selenium/webdriver/common/service.py”, 第111行,断言进程中仍在运行 %(self.path,return_code)selenium.common.exceptions.WebDriverException:Message:Service /opt/example1/assets/chromedriver意外退出。状态码 was:127

有人知道我该怎么做才能防止这个错误吗? 是什么导致了这次事故

以下是发生错误的初始化代码:

CHROME_DRIVER_PATH = os.path.abspath('assets/chromedriver')


class MdCrawler(Crawler):

def __init__(self, start_url, source):
    super().__init__(start_url, source)
    serialized_arr = self.read_data_from_json_file(JSON_FILE_PATH)
    self.sent_ids = [] if serialized_arr is None else serialized_arr
    >>> self.web_driver_chrome = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH)
    exit(1)
编辑1:

我已经编辑了Dockerfile(添加了ubuntu:17.04和aptget libnss3 libgconf-2-4)。在构建docker映像后,我遇到了不同的错误:

selenium.common.exceptions.WebDriverException:消息:未知错误: 找不到Chrome二进制文件(驱动程序信息:chromedriver=2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5),平台=Linux 4.9.125-linuxkit x86_64)

编辑2:

我补充说

RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | tee /etc/apt/sources.list.d/google-chrome.list
RUN apt-get update
RUN apt-get install -y google-chrome-stable
到我的Dockerfile,但出现新错误:

引发异常类(消息、屏幕、堆栈跟踪)selenium.common.exceptions.WebDriverException:消息:未知错误: Chrome启动失败:异常退出(未知错误: DevToolsActivePort文件不存在)(进程从 chrome location/usr/bin/google chrome不再运行,因此 ChromeDriver假设Chrome已崩溃。)(驾驶员信息: chromedriver=2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5),平台=Linux 4.9.125-linuxkit x86_64)

你没有忘记添加无头模式吗


Docker容器中Selenium chromedriver的最小测试脚本如下所示:

import selenium.webdriver

options = selenium.webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')

driver = selenium.webdriver.Chrome(chrome_options=options)
driver.get('https://www.python.org/')
print(driver.title)
driver.close()

因此,看起来您缺少了
--headless
--no sandbox
参数。

docker run imagename/opt/example1/assets/chromedriver
可能提供了信息;到目前为止,您显示的错误消息只是说这不起作用。@DavidMaze这是我的输出:(carcrawler)Lukas-MacBook-Pro-2:carcrawler lukadragicevic$docker运行lukatest/opt/example1/assets/chromedriver/opt/example1/assets/chromedriver:加载共享库时出错:libnss3.so:无法打开共享对象文件:没有此类文件或directory@DavidMaze我已经更新了我的帖子(见编辑1)。感谢您的回复。我忘了,现在我已经用这部分代码进行了测试,但我遇到了相同的错误。我现在有完全相同的代码,并且运行良好,但我从目标网站(mobile.de)收到消息:“不幸的是,自动访问此页面被拒绝。”。我怎样才能通过这个街区?
import selenium.webdriver

options = selenium.webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')

driver = selenium.webdriver.Chrome(chrome_options=options)
driver.get('https://www.python.org/')
print(driver.title)
driver.close()