Docker Selenium:selenium.common.exceptions.WebDriverException:消息:服务chromedriver意外退出.状态码为:127 [英] Docker Selenium: selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127

查看:310
本文介绍了Docker Selenium:selenium.common.exceptions.WebDriverException:消息:服务chromedriver意外退出.状态码为:127的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python项目中使用了硒的chromedriver.

I am using selenium's chromedriver in my python project.

我正在成功构建我的Dockerfile:

I am building successfully my 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容器时,出现以下错误:

But when I run my docker container I got following error:

回溯(最近一次通话最后一次):文件"program.py",第8行,在 MdCrawler(MD_START_URL,"MobileDe").start()文件"/opt/example1/mobile_de_crawler.py",第17行, init self.web_driver_chrome = webdriver.Chrome(executable_path = CHROME_DRIVER_PATH)文件 "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", 第73行,初始化 self.service.start()文件"/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", 第98行,开始时 self.assert_process_still_running()文件"/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", 第111行,在assert_process_still_running中 %(self.path,return_code) selenium.common.exceptions.WebDriverException:消息:服务 /opt/example1/assets/chromedriver意外退出.状态码 原为:127

Traceback (most recent call last): File "program.py", line 8, in MdCrawler(MD_START_URL, "MobileDe").start() File "/opt/example1/mobile_de_crawler.py", line 17, in init self.web_driver_chrome = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH) File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in init self.service.start() File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 98, in start self.assert_process_still_running() File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 111, in assert_process_still_running % (self.path, return_code) selenium.common.exceptions.WebDriverException: Message: Service /opt/example1/assets/chromedriver unexpectedly exited. Status code was: 127

任何人都知道我该怎么做才能防止此错误? 是什么原因导致此崩溃?

Anyone got an idea what could I do to prevent this error? What is causing this crash?

这是我的发生错误的初始化代码:

Here is my initialization code where error occurs:

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)

我已经编辑了Dockerfile(添加了ubuntu:17.04和aptget libnss3 libgconf-2-4).构建了我的docker镜像后,出现了另一个错误:

I have edited Dockerfile (added ubuntu:17.04 and aptget libnss3 libgconf-2-4). After building my docker image, I got different error:

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

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary (Driver info: chromedriver=2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5),platform=Linux 4.9.125-linuxkit x86_64)

我已添加

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,但是新错误即将出现:

To my Dockerfile, but new error is coming:

引发exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.WebDriverException:消息:未知错误: Chrome无法启动:异常退出(未知错误: DevToolsActivePort文件不存在)(该过程始于 chrome位置/usr/bin/google-chrome不再运行,因此 ChromeDriver假设Chrome已崩溃.)(驱动程序信息: chromedriver = 2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5),平台= Linux 4.9.125-linuxkit x86_64)

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) (Driver info: chromedriver=2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5),platform=Linux 4.9.125-linuxkit x86_64)

推荐答案

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

My minimal test script for Selenium chromedriver inside my Docker container looks like this:

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自变量.

So it looks like you're missing --headless and --no-sandbox arguments.

这篇关于Docker Selenium:selenium.common.exceptions.WebDriverException:消息:服务chromedriver意外退出.状态码为:127的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆