簡體   English   中英

selenium.common.exceptions.WebDriverException:消息:未知錯誤:Chrome 無法啟動:異常退出。 Dockerize Flask 應用程序

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

一個多星期以來,我一直在嘗試將 Flask 應用程序與 MongoDB 數據庫對接。 我的應用程序具有以下結構:

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

此應用程序使用 selenium、selenium-requests 和 BeautifulSoup 庫進行 web 抓取。 我一周以來遇到的問題是:

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,但我找不到真正有效的方法。

我的代碼如下:

  • 應用程序.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})
  • 數據庫 Dockerfile
FROM mongo:5.0.3
  • 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"]
  • 要求.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 在 /usr/local/bin 文件夾中創建 chromedriver,在 /usr/bin 文件夾中創建 google-chrome 和 google-chrome-stable 文件。

所以我不明白為什么,即使經過這么多不同的嘗試,我也無法讓這段代碼工作......有人可以幫我嗎?

此錯誤消息...

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 瀏覽器session。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 selenium.common.exceptions.WebDriverException:消息:未知錯誤:Chrome無法啟動:使用ChromeDriver Chrome和Selenium異常退出 TDD-Django(部署)錯誤:selenium.common.exceptions.WebDriverException:消息:未知錯誤:Chrome無法啟動:異常退出 selenium.common.exceptions.WebDriverException:消息:未知錯誤:Chrome無法以Selenium和RaspberryPi上的Chrome啟動 selenium.common.exceptions.WebDriverException:消息:未知錯誤:Chrome 無法啟動:在 Python 中使用 ChromeDriver 和 Selenium 崩潰 Selenium.common.exceptions.WebDriverException:消息:未知錯誤:沒有 chrome 二進制文件 selenium.common.exceptions.WebDriverException:消息:未知錯誤:無法使用 ChromeDriver Chrome Selenium 創建 Chrome 進程錯誤 selenium.common.exceptions.WebDriverException:消息:未知錯誤:在ubuntu上執行selenium python腳本時,chrome無法啟動 selenium.common.exceptions.WebDriverException:消息:未知錯誤:無法使用帶有Selenium Python的ChromeDriver Chrome創建Chrome進程 WebDriverException:消息:未知錯誤:Chrome 無法啟動:使用 ChromeDriver Chrome 和 Selenium Python 異常退出錯誤 設置 selenium 與 headless chrome 錯誤:WebDriverException:消息:未知錯誤:Chrome 無法啟動:異常退出
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM