我正在尝试在我的应用程序的 Python 映像上安装 selenium / chromedriver。我尝试了很多不同的迭代,但似乎没有任何效果。
需求.txt
selenium==4.23.1
webdriver-manager==4.0.2
我使用的是苹果芯片。这是我的 Dockerfile。现在有点混乱,因为我添加了各种依赖项,但似乎没有任何效果。
FROM --platform=linux/amd64 python:3.12-slim
WORKDIR /app
RUN apt-get update && apt-get install -y \
wget \
curl \
gnupg \
unzip \
apt-transport-https \
ca-certificates \
libnss3 \
libgconf-2-4 \
libx11-xcb1 \
libxcomposite1 \
libxcursor1 \
libxdamage1 \
libxi6 \
libxtst6 \
libxrandr2 \
libasound2 \
libpango1.0-0 \
libpangocairo-1.0-0 \
libcups2 \
libxss1 \
libgtk-3-0 \
--no-install-recommends
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb || true
# Fix broken dependencies (if any)
RUN apt-get -f install -y
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt
COPY . /app/
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
LANG=C.UTF-8 \
LC_ALL=C.UTF-8
构建镜像,启动容器:
docker build . -t my-image
docker run -it --shm-size=2g my-image
在 Python shell 中:
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--disable-dev-shm-usage")
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)
错误是:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.12/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
super().__init__(
File "/usr/local/lib/python3.12/site-packages/selenium/webdriver/chromium/webdriver.py", line 66, in __init__
super().__init__(command_executor=executor, options=options)
File "/usr/local/lib/python3.12/site-packages/selenium/webdriver/remote/webdriver.py", line 212, in __init__
self.start_session(capabilities)
File "/usr/local/lib/python3.12/site-packages/selenium/webdriver/remote/webdriver.py", line 299, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/selenium/webdriver/remote/webdriver.py", line 354, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.12/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: disconnected: Unable to receive message from renderer
(failed to check if window was closed: disconnected: not connected to DevTools)
(Session info: chrome=128.0.6613.84)
Stacktrace:
#0 0x555555daa81a <unknown>
#1 0x555555a78e50 <unknown>
#2 0x555555a60e20 <unknown>
#3 0x555555a5eb11 <unknown>
#4 0x555555a5f31f <unknown>
#5 0x555555a799f1 <unknown>
#6 0x555555a4de89 <unknown>
#7 0x555555a4d7d6 <unknown>
#8 0x555555af99db <unknown>
#9 0x555555af8e66 <unknown>
#10 0x555555aed233 <unknown>
#11 0x555555abb093 <unknown>
#12 0x555555abc09e <unknown>
#13 0x555555d71a7b <unknown>
#14 0x555555d75a31 <unknown>
#15 0x555555d5d645 <unknown>
#16 0x555555d765a2 <unknown>
#17 0x555555d4281f <unknown>
#18 0x555555d99618 <unknown>
#19 0x555555d997e2 <unknown>
#20 0x555555da960c <unknown>
#21 0x2aaaab7ab134 <unknown>
尝试将这些依赖项添加到 dockerfile 中:
xvfb \
fonts-liberation \
libappindicator3-1 \
xdg-utils \
如果仍然无法正常工作,可以通过将此配置添加到 chrome 驱动程序来查看日志:
chrome_options.add_argument("--enable-logging")
chrome_options.add_argument("--v=1")