简体   繁体   English

selenium.common.exceptions.WebDriverException:消息:chromedriver 意外退出。 状态码为:255 使用 Dockerfile 时

[英]selenium.common.exceptions.WebDriverException: Message: chromedriver unexpectedly exited. Status code was: 255 When using Dockerfile

I have a Python Webscraping app that works flawlessly on my localhost (MacOS, M1 Silicon).我有一个 Python Webscraping 应用程序,可以在我的本地主机(MacOS,M1 Silicon)上完美运行。

I am trying to publish it into Azure using a Container.我正在尝试使用容器将其发布到 Azure 中。

The problem:问题:

When I build my app using a Dockerfile - the containerised image produces an error:当我使用 Dockerfile 构建我的应用程序时,容器化图像会产生错误:

selenium.common.exceptions.WebDriverException: Message: Service /root/.wdm/drivers/chromedriver/linux64/100.0.4896.60/chromedriver unexpectedly exited. Status code was: 255

The Dockerfile: Dockerfile:

Here is the Dockerfile that I've created:这是我创建的 Dockerfile:

FROM python:3.9-buster
# FROM --platform=linux/amd64  python:3.9

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

RUN apt-get update \
    && apt-get -y install gcc make \
    && rm -rf /var/lib/apt/lists/*s

# RUN apt-get update
RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
RUN apt-get install -y chromium
# RUN apt-get install -y chromium-browser

# install manually all the missing libraries
RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils

# install chrome
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
# RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install

RUN dpkg -i google-chrome-stable_current_amd64.deb --fix-missing; apt-get -fy install


RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
RUN apt-get -y update

RUN apt-get update && apt-get install -y wget bzip2 libxtst6 packagekit-gtk3-module libx11-xcb-dev libdbus-glib-1-2 libxt6 libpci-dev && rm -rf /var/lib/apt/lists/*

#download and install chrome
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install

RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
RUN apt-get install -y chromium

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 update -y
RUN apt install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
# RUN apt install -y google-chrome-stable


RUN apt-get install -yqq unzip
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/
RUN python3 --version
RUN pip3 --version
RUN pip install --no-cache-dir --upgrade pip


#install python dependencies
COPY requirements.txt requirements.txt
RUN pip install -r ./requirements.txt

#some envs
ENV APP_HOME /app
ENV PORT 5000

#set workspace
WORKDIR ${APP_HOME}

#copy local files
COPY . .

CMD exec gunicorn --bind :${PORT} --workers 1 --threads 8 main:app
# CMD exec gunicorn -b :$PORT main:app
# EXPOSE 8080
# CMD ["gunicorn", "--bind", "0.0.0.0:8080","--timeout", "90", "main:main"]
# CMD ["python3", "main.py"]
#build using:
# docker build -t python-webscraper .
# docker run --rm -p 3500:5000 python-webscraper
# docker run -p 3500:5000 python-webscraper

Calling ChromeDriver from Code从代码调用 ChromeDriver

chromedriver_autoinstaller.install()  # Check if the current version of chromedriver exists
    # and if it doesn't exist, download it automatically,
    # then add chromedriver to path
    chrome_options = Options()
    chrome_options.add_argument("--no-sandbox")
    # chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--headless")
    # chrome_options.add_argument("window-size=1400,2100")
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument("--disable-setuid-sandbox")
    chrome_options.add_argument('--disable-dev-shm-usage')  # Not used

As you can see, I'm using the headless option and no sandbox options to pass into the driver.如您所见,我使用无头选项和无沙盒选项传递给驱动程序。

How am I building the container我如何构建容器

I appreciate Im using MacOs, M1 chip, so I've tried to build using the following 2 ways:我很欣赏我使用 MacOs、M1 芯片,所以我尝试使用以下 2 种方式进行构建:

docker buildx build --platform linux/amd64 -t app2 .

or或者

docker build -t app2

Then I just run it using然后我只是使用

 docker run -it  app2

Summary概括

  1. Im creating a local containerised image of my app to test it before publishing it to Azure.我创建了我的应用程序的本地容器化映像以在将其发布到 Azure 之前对其进行测试。
  2. The Dockerfile creates the container for me. Dockerfile 为我创建了容器。
  3. When I test it (run the image) locally - the web scraping app fails when launching Chrome Driver.当我在本地测试它(运行图像)时 - web 抓取应用程序在启动 Chrome 驱动程序时失败。

I'm now really desperate and have tried a lot of searching and refactoring without any luck.我现在真的很绝望,并且尝试了很多搜索和重构,但没有任何运气。

I had this same issue.我有同样的问题。 This won't work for mac but works for ubuntu.这不适用于 mac,但适用于 ubuntu。 You need to install Google Chrome and set the path to it (options.binary_location = "/usr/bin/google-chrome").您需要安装 Google Chrome 并设置它的路径(options.binary_location = "/usr/bin/google-chrome")。

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service


service = Service(executable_path=ChromeDriverManager().install())
options = Options()
options.add_argument('--disable-blink-features=AutomationControlled') ## to avoid getting detected
options.add_argument('headless')
options.binary_location = "/usr/bin/google-chrome-stable" # <<<<---- this row should contain path to Browser's binary location to make solve your issue
driver = webdriver.Chrome(service=service, options=options)"

暂无
暂无

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

相关问题 Docker Selenium:selenium.common.exceptions.WebDriverException:消息:服务chromedriver意外退出。 状态码为:127 - Docker Selenium: selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127 selenium.common.exceptions.WebDriverException:消息:服务 chromedriver 意外退出。 状态码是:1 - selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1 webDriverException“selenium.common.exceptions.WebDriverException:消息:Service./webdrivers/geckodriver 意外退出。状态代码为:1” - webDriverException "selenium.common.exceptions.WebDriverException: Message: Service ./webdrivers/geckodriver unexpectedly exited. Status code was: 1" selenium.common.exceptions.WebDriverException:消息:Service.\geckodriver.exe 意外退出。 状态码为:3221225595 - selenium.common.exceptions.WebDriverException: Message: Service .\geckodriver.exe unexpectedly exited. Status code was: 3221225595 selenium.common.exceptions.WebDriverException:消息:服务意外退出。 状态码为:3221225595 - selenium.common.exceptions.WebDriverException: Message: Service unexpectedly exited. Status code was: 3221225595 selenium.common.exceptions.WebDriverException:消息:服务geckodriver意外退出。 状态代码为:-11-如何解决? - selenium.common.exceptions.WebDriverException: Message: Service geckodriver unexpectedly exited. Status code was: -11 - How to fix? selenium.common.exceptions.WebDriverException:消息:服务geckodriver意外退出。 状态码为:69 - selenium.common.exceptions.WebDriverException: Message: Service geckodriver unexpectedly exited. Status code was: 69 selenium.common.exceptions.WebDriverException:消息:服务 chromedriver 意外退出 - selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited WebDriverException:消息:服务 chromedriver 意外退出。 状态代码为:127 - WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127 WebDriverException:消息:服务 /content/chromedriver 意外退出。 状态代码为:-6 使用 ChromeDriver Google Colab 和 Selenium - WebDriverException: Message: Service /content/chromedriver unexpectedly exited. Status code was: -6 with ChromeDriver Google Colab and Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM