简体   繁体   English

selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome 无法启动:异常退出。 Dockerize Flask 应用程序

[英]selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally. Dockerize Flask Application

I have been trying for over a week to dockerize a Flask application with the MongoDB database.一个多星期以来,我一直在尝试将 Flask 应用程序与 MongoDB 数据库对接。 My application has the following structure:我的应用程序具有以下结构:

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

This application makes web scraping using the selenium, selenium-requests and BeautifulSoup libraries.此应用程序使用 selenium、selenium-requests 和 BeautifulSoup 库进行 web 抓取。 The problem I've been getting for a week is:我一周以来遇到的问题是:

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

I've been trying several different ways to make the application's Dockerfile and I can't find one that really works.我一直在尝试几种不同的方法来制作应用程序的 Dockerfile,但我找不到真正有效的方法。

My code is as follows:我的代码如下:

  • app.py应用程序.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数据库 Dockerfile
FROM mongo:5.0.3
  • web Dockerfile web 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要求.txt
flask
flask-restful
pymongo
bs4
selenium
selenium-requests
lxml
webdriver-manager

and

  • docker-compose.yml docker-compose.yml
version: '3.9'

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

With the web folder dockerfile the chromedriver is created in the /usr/local/bin folder and the google-chrome and google-chrome-stable files in the /usr/bin folder.使用 web 文件夹 dockerfile 在 /usr/local/bin 文件夹中创建 chromedriver,在 /usr/bin 文件夹中创建 google-chrome 和 google-chrome-stable 文件。

So I can't understand why, even after so many different attempts, I can't get this code to work... Could someone help me please?所以我不明白为什么,即使经过这么多不同的尝试,我也无法让这段代码工作......有人可以帮我吗?

This error message...此错误消息...

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

...implies that the ChromeDriver was unable to initiate/spawn a new WebBrowser ie Chrome Browser session. ...意味着ChromeDriver无法启动/生成新的WebBrowser ,即Chrome 浏览器session。

A common cause for Chrome to crash during startup is running Chrome as root user ( administrator ) on Linux. Chrome 在启动期间崩溃的一个常见原因是在 Linux 上以root用户( administrator )身份运行 Chrome。 While it is possible to work around this issue by passing --no-sandbox flag when creating your WebDriver session, such a configuration is unsupported and highly discouraged.虽然可以通过在创建 WebDriver session 时传递--no-sandbox标志来解决此问题,但不支持并且强烈建议不要使用此类配置。 You need to configure your environment to run Chrome as a regular user instead.您需要将环境配置为以普通用户身份运行 Chrome。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome无法启动:使用ChromeDriver Chrome和Selenium异常退出 - selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally with ChromeDriver Chrome and Selenium TDD-Django(部署)错误:selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome无法启动:异常退出 - TDD-Django(deploy) Error : selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome无法以Selenium和RaspberryPi上的Chrome启动 - selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start with Selenium and Chrome on RaspberryPi selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome 无法启动:在 Python 中使用 ChromeDriver 和 Selenium 崩溃 - selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed with ChromeDriver and Selenium in Python Selenium.common.exceptions.WebDriverException:消息:未知错误:没有 chrome 二进制文件 - Selenium.common.exceptions.WebDriverException: Message: unknown error: no chrome binary selenium.common.exceptions.WebDriverException:消息:未知错误:无法使用 ChromeDriver Chrome Selenium 创建 Chrome 进程错误 - selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to create a Chrome process error with ChromeDriver Chrome Selenium selenium.common.exceptions.WebDriverException:消息:未知错误:在ubuntu上执行selenium python脚本时,chrome无法启动 - selenium.common.exceptions.WebDriverException: Message: unknown error: chrome failed to start while executing selenium python script on ubuntu selenium.common.exceptions.WebDriverException:消息:未知错误:无法使用带有Selenium Python的ChromeDriver Chrome创建Chrome进程 - selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to create Chrome process with ChromeDriver Chrome with Selenium Python WebDriverException:消息:未知错误:Chrome 无法启动:使用 ChromeDriver Chrome 和 Selenium Python 异常退出错误 - WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally error using ChromeDriver Chrome and Selenium Python 设置 selenium 与 headless chrome 错误:WebDriverException:消息:未知错误:Chrome 无法启动:异常退出 - Setup selenium with headless chrome error: WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM