Докер: настройка драйвера Selenium ChromePython

Программы на Python
Ответить
Anonymous
 Докер: настройка драйвера Selenium Chrome

Сообщение Anonymous »

Я много раз пытался настроить свой докер с помощью селена, и мне нужна ваша помощь.
Мой фактический файл докера:

Код: Выделить всё

# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

ARG PYTHON_VERSION=3.11.4
FROM python:${PYTHON_VERSION}-slim AS base

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1

# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1

WORKDIR /app

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
# Leverage a bind mount to requirements.txt to avoid having to copy them into
# into this layer.
RUN pip install --upgrade pip
RUN --mount=type=cache,target=/root/.cache/pip \
--mount=type=bind,source=requirements.txt,target=requirements.txt \
python -m pip install -r requirements.txt

#####################

RUN apt-get update
RUN apt-get install wget -y
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

#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

#####################
# Set environment variables to prevent Firefox from using a display
ENV DISPLAY=:99

# Switch to the non-privileged user to run the application.
USER appuser

# Copy the source code into the container.
COPY .  .

# Expose the port that the application listens on.
EXPOSE 8000

# Run the application.
CMD ["python", "app.py"]
и мой код:

Код: Выделить всё

from selenium import webdriver
# import chromedriver_binary
from webdriver_manager.chrome import ChromeDriverManager

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-audio")

# Configure o WebDriver para usar o Selenium Standalone Chrome
driver = webdriver.Chrome(ChromeDriverManager().install())
с помощью compose.yaml следующим образом:

Код: Выделить всё

services:
server:
build:
context: .
ports:
- "8000:8000"`
и, наконец, мой журнал:

Код: Выделить всё

 Traceback (most recent call last):
mercadolivrescraping-server-1  |   File "/app/app.py", line 15, in 
mercadolivrescraping-server-1  |     driver = webdriver.Chrome(ChromeDriverManager().install())
mercadolivrescraping-server-1  |
mercadolivrescraping-server-1  |
mercadolivrescraping-server-1  |    ^^
mercadolivrescraping-server-1  | ^^^^^
mercadolivrescraping-server-1  | ^^^^^^^^
mercadolivrescraping-server-1  | ^^^^^
mercadolivrescraping-server-1  | ^^^^^^
mercadolivrescraping-server-1  | ^^^^^
mercadolivrescraping-server-1  |   File "/usr/local/lib/python3.11/site-packages/webdriver_manager/chrome.py", line 40, in install
mercadolivrescraping-server-1  |     driver_path = self._get_driver_binary_path(self.driver)
mercadolivrescraping-server-1  |
mercadolivrescraping-server-1  |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
mercadolivrescraping-server-1  |   File "/usr/local/lib/python3.11/site-packages/webdriver_manager/core/manager.py", line 41, in _get_driver_binary_path
mercadolivrescraping-server-1  |     binary_path = self._cache_manager.save_file_to_cache(driver, file)
mercadolivrescraping-server-1  |
mercadolivrescraping-server-1  |  ^^^^^^^
mercadolivrescraping-server-1  | ^^^^^
mercadolivrescraping-server-1  | ^^^^^^^
mercadolivrescraping-server-1  | ^^^^^^
mercadolivrescraping-server-1  | ^^^^
mercadolivrescraping-server-1  | ^^^^^^^^^^^^^^^^^^^^^^^
mercadolivrescraping-server-1  |   File "/usr/local/lib/python3.11/site-packages/webdriver_manager/core/driver_cache.py", line 53, in save_file_to_cache
mercadolivrescraping-server-1  |     archive = self.save_archive_file(file, path)
mercadolivrescraping-server-1  |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
mercadolivrescraping-server-1  |   File "/usr/local/lib/python3.11/site-packages/webdriver_manager/core/driver_cache.py", line 46, in save_archive_file
mercadolivrescraping-server-1  |     return self._file_manager.save_archive_file(file, path)
mercadolivrescraping-server-1  |            ^^^^
mercadolivrescraping-server-1  | ^^^^^^^^^^^^^^^^^^^^
mercadolivrescraping-server-1  | ^^^^^^^^^
mercadolivrescraping-server-1  | ^^^^^^
mercadolivrescraping-server-1  | ^^^^^^^
mercadolivrescraping-server-1  | ^^
mercadolivrescraping-server-1  |   File "/usr/local/lib/python3.11/site-packages/webdriver_manager/core/file_manager.py", line 45, in save_archive_file
mercadolivrescraping-server-1  |
mercadolivrescraping-server-1  | os.makedirs(directory, exist_ok=True)
mercadolivrescraping-server-1  |   File "", line 215, in makedirs
mercadolivrescraping-server-1  |   File "", line 215, in makedirs
mercadolivrescraping-server-1  |   File "", line 215, in makedirs
mercadolivrescraping-server-1  |   [Previous line repeated 2 more times]
mercadolivrescraping-server-1  |   File "", line 225, in makedirs
mercadolivrescraping-server-1  | PermissionError: [Errno 13] Permission denied: '/nonexistent'
Я пробовал установить Chrome и драйвер Chrome вручную, но это не сработало. Я попробовал библиотеку chromedriver_binary из Python с хромом, установленным с помощью файла docker, и это не сработало. и я пробовал так много вещей, и тоже не помогло.
Можете ли вы сказать мне, что я делаю неправильно и что я могу сделать вместо этого, пожалуйста? Если вам нужна дополнительная информация, спросите меня.

Подробнее здесь: https://stackoverflow.com/questions/792 ... iver-setup
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»