Драйвер Selenium Chrome в контейнере Docker не запускается

Я новичок в докере. Мне нужно очистить веб-сайт с помощью драйвера Selenium Chrome от Python из контейнера докера.

Докерфайл:

FROM python:3.11-slim-buster
# Install dependencies
RUN apt-get update && apt-get install -y wget unzip
# Download ChromeDriver
RUN wget https://storage.googleapis.com/chrome-for-testing-public/128.0.6613.84/win32/chrome-win32.zip
RUN unzip chrome-win32.zip -d /usr/local/bin/
# Copy your project files
COPY . /app
USER root
# Set the working directory
WORKDIR /app
# Install Python dependencies
RUN pip install selenium==3.141.0
RUN pip install urllib3==1.26.16
RUN pip install webdriver-manager
CMD ["python3", "seleniumdriver.py"]

Код Python:

from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("--headless")
chromeOptions.add_argument("--no-sandbox")
driver = webdriver.Chrome(
    chrome_options=chromeOptions
)
driver.get("https://www.google.com/")
driver.quit()

Сборка Docker: docker -t seleniumdriver. Docker Run: драйвер docker selenium ОС: Windows 10

Сборка Docker прошла успешно, но запуск Docker выдает ошибку.

raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

Пробовал разные подходы, но мне ничего не помогло.


63
1

Ответ:

Решено

Небольшое изменение заставило это работать.

Докерфайл

От

...
RUN apt-get update && apt-get install -y wget unzip
# Download ChromeDriver
RUN wget https://storage.googleapis.com/chrome-for-testing-public/128.0.6613.84/win32/chrome-win32.zip
RUN unzip chrome-win32.zip -d /usr/local/bin/
...

К

RUN apt-get update && apt-get install -y wget unzip chromium-driver

Кроме того, чтобы убедиться, что эти изменения работают, я заставляю браузер сделать снимок экрана.

Для проверки вы можете использовать следующие шаги.

Докерфайл

FROM python:3.11-slim-buster
# Install dependencies
RUN apt-get update && apt-get install -y wget unzip chromium-driver
# Copy your project files
COPY . /app
USER root
# Set the working directory
WORKDIR /app
# Install Python dependencies
RUN pip install selenium==3.141.0
RUN pip install urllib3==1.26.16
RUN pip install webdriver-manager

VOLUME ["/app"]

CMD ["python3", "seleniumdriver.py"]

селендрайвер.py

from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("--headless")
chromeOptions.add_argument("--no-sandbox")
driver = webdriver.Chrome(
    options=chromeOptions
)
driver.get("https://www.google.com/")
driver.get_screenshot_as_file("screenshot.png")
driver.quit()

Команды

$ docker build -t seleniumdriver .
$ docker run --name seleniumdriver -p 80:80 -v .:/app seleniumdriver 

Вы могли видеть screenshot.png в той же папке.