【问题标题】:selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally. Dockerize Flask Applicationselenium.common.exceptions.WebDriverException:消息:未知错误:Chrome 无法启动:异常退出。 Dockerize 烧瓶应用程序
【发布时间】:2022-01-15 13:28:02
【问题描述】:

一个多星期以来,我一直在尝试使用 MongoDB 数据库对 Flask 应用程序进行 dockerize。我的应用程序具有以下结构:

aplication
   - db
     Dockerfile
   - web
     app.py
     Dockerfile
     requirements.txt
   docker-compose.yml

此应用程序使用 selenium、selenium-requests 和 BeautifulSoup 库进行网络抓取。我一周以来遇到的问题是:

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.)

我一直在尝试几种不同的方法来制作应用程序的 Dockerfile,但我找不到真正有效的方法。

我的代码如下:

  • app.py
from bs4 import BeautifulSoup
from seleniumrequests import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from flask import Flask, request, jsonify
from flask_restful import API, Resource
from pymongo import MongoClient

app = Flask(__name__)
api = api(app)

client = MongoClient("mongodb://db:27017")
db = client.FlaskAPI

users = db["users"]

chrome_options = Options()
chrome_options.add_argument("--headless")
webdriver = Chrome(executable_path='/usr/local/bin/chromedriver', options=chrome_options)
webdriver.get(self.url+'login')
webdriver.find_element_by_id("userName-id").send_keys(username)
webdriver.find_element_by_id("passWd-id").send_keys(password)
        
webdriver.find_element(By.XPATH, '//input[@value="Login"]').click()
soup = BeautifulSoup(self.webdriver.page_source, 'lxml')

data = ''
        
        p = re.compile('var userParam=(.*);')        
        for script in soup.find_all("script", {"src":False}):
            if p.search(script.string):
                data = json.dumps(str(script.string))

data = remove_formatation_elements(data)
users.insert({"data": data})
  • db Dockerfile
FROM mongo:5.0.3
  • 网络 Dockerfile
FROM python:3.8

WORKDIR /usr/src/app

# Install Chrome WebDriver
RUN CHROMEDRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \
    mkdir -p /opt/chromedriver-$CHROMEDRIVER_VERSION && \
    curl -sS -o /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip && \
    unzip -qq /tmp/chromedriver_linux64.zip -d /opt/chromedriver-$CHROMEDRIVER_VERSION && \
    rm /tmp/chromedriver_linux64.zip && \
    chmod +x /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver && \
    ln -fs /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver /usr/local/bin/chromedriver

# Install Google Chrome
RUN curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
    echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list && \
    apt-get -yqq update && \
    apt-get -yqq install google-chrome-stable && \
    rm -rf /var/lib/apt/lists/*

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "app.py"]
  • requirements.txt
flask
flask-restful
pymongo
bs4
selenium
selenium-requests
lxml
webdriver-manager

  • docker-compose.yml
version: '3.9'

services:
  web:
    build: './web'
    ports:
      - '5000:5000'
    links:
      - db
  
  db:
    build: './db'

使用 web 文件夹 dockerfile,chromedriver 在 /usr/local/bin 文件夹中创建,google-chrome 和 google-chrome-stable 文件在 /usr/bin 文件夹中创建。

所以我不明白为什么,即使经过这么多不同的尝试,我也无法让这段代码工作...... 有人可以帮帮我吗?

【问题讨论】:

    标签: python docker selenium google-chrome selenium-webdriver


    【解决方案1】:

    此错误消息...

    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.)
    

    ...暗示ChromeDriver 无法启动/生成新的WebBrowser,即Chrome 浏览器 会话。

    Chrome 在启动期间崩溃的一个常见原因是在 Linux 上以 root 用户 (administrator) 运行 Chrome。虽然可以通过在创建 WebDriver 会话时传递 --no-sandbox 标志来解决此问题,但不支持并且强烈建议不要使用此类配置。您需要将环境配置为以普通用户身份运行 Chrome。

    【讨论】:

    • 很抱歉询问,但我做了一些研究,仍然没有找到以普通用户身份运行 Chrome 的方法。请问有什么参考资料可以用来码头化这个 api 吗?
    猜你喜欢
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 2017-10-23
    • 2017-09-07
    • 2014-04-20
    • 2014-04-28
    • 2020-02-05
    • 2016-10-08
    相关资源
    最近更新 更多