python - Docker Selenium : selenium. common.exceptions.WebDriverException : Message: Service chromedriver unexpectedly exited. 状态代码为:127

标签 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 -r /tmp/requirements.txt
ADD . /opt/example1/
# rights?
RUN chmod +x /opt/example1/assets/chromedriver
WORKDIR /opt/example1
CMD ["python","-u","program.py"]

但是当我运行我的 docker 容器时,出现以下错误:

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

有人知道我可以做什么来防止这个错误吗? 是什么导致了这次崩溃?

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

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: Message: unknown error: cannot find Chrome binary (Driver info: chromedriver=2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5),platform=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,但出现新错误:

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 最小测试脚本如下所示:

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 参数。

关于python - Docker Selenium : selenium. common.exceptions.WebDriverException : Message: Service chromedriver unexpectedly exited. 状态代码为:127,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54043221/

相关文章:

selenium - Webdriver 从 bamboo agent 得到错误的解决方案

docker - 同步洪水和 net.ipv4.tcp_syncookies

mysql - apt-get : command not found in mysql docker CLI

python - 智能自动抓取

python - Django Haystack SOLR 中的排序顺序

java - 为什么我每次尝试使用 selenium 测试脚本登录 Naukari.com 时都会收到 1001 错误?

java - 目前无法识别列表中选中/选定的单选按钮

docker - 如何访问 docker-compose 中的所有 nginx 日志?

Python:确保我的类只使用最新的协议(protocol)进行 pickle

python - 如何让 HMM 处理 Tensorflow 中的实值数据