我在尝试使用 selenium 启动 chrome webdriver 时遇到了一些问题。我不太熟悉 Docker 或 Lightsail。当我在本地计算机上运行我的代码时,它可以正常工作。但是,我为要运行代码的 Flask 服务创建了一个 Docker 容器。我的 Flask Web 应用程序中的 html 数据包含一个启动网络爬虫的按钮,在我使用 Flask 服务之前,它可以正常工作。我让 Docker 容器正常工作,所以我不确定我的 webdriver 和 chrome 之间是否存在版本控制不匹配,或者其他一些我没有看到的问题。当我尝试通过我的 Lightsail Flask 服务使用 Selenium 启动 chrome 时,出现以下错误:
super().__init__(
File "/usr/local/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 61, in __init__
super().__init__(command_executor=executor, options=options)
File "/usr/local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 208, in __init__
self.start_session(capabilities)
File "/usr/local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 292, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 347, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Chrome failed to start: exited abnormally.
(timeout: Timed out receiving message from renderer: 60.000)
(The process started from chrome location /opt/google/chrome/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Stacktrace:
如您所见,chrome webdriver 从未启动,我不知道为什么。这是我的项目基础镜像的 docker 文件(我有一个基础镜像和第二个基础镜像)。
# Set base image (host OS)
FROM debian:latest
# Install necessary dependencies
RUN apt-get update -y \
&& apt-get upgrade -y \
&& apt-get install -y \
wget \
gnupg \
unzip \
xz-utils \
build-essential \
zlib1g-dev \
libncurses5-dev \
libgdbm-dev \
libssl-dev \
libreadline-dev \
libffi-dev \
libsqlite3-dev \
libbz2-dev \
libnss3-dev \
openssl \
&& export DEBIAN_FRONTEND=noninteractive
# Install Chrome
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list \
&& apt-get update && apt-get install -y \
google-chrome-stable
# Install Chrome WebDriver - seems to get installed with Chrome
#RUN wget -q -O /usr/local/bin/chromedriver https://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_linux64.zip \
# && unzip /usr/local/bin/chromedriver -d /usr/local/bin \
# && chmod +x /usr/local/bin/chromedriver
# Install Python 3.11.3
RUN wget -O /tmp/python.tar.xz https://www.python.org/ftp/python/3.11.3/Python-3.11.3.tar.xz \
&& mkdir -p /usr/src/python \
&& tar -xJC /usr/src/python --strip-components=1 -f /tmp/python.tar.xz \
&& rm /tmp/python.tar.xz \
&& cd /usr/src/python \
&& ./configure --enable-optimizations --with-ssl \
&& make -j$(nproc) \
&& make install \
&& cd / \
&& rm -rf /usr/src/python \
&& python3 -c "import ssl; print(ssl.OPENSSL_VERSION)"
# Update pip
RUN pip3 install --upgrade pip
# Clean up
RUN rm -rf /var/lib/apt/lists/*
# Copy the dependencies file to the working directory
COPY requirements.txt /tmp/
# Install python dependencies
RUN pip3 install -r /tmp/requirements.txt
这是我的要求文件:
flask===2.2.2
matplotlib===3.8.0
numpy===1.24.3
pandas===1.5.3
pytz===2023.3
selenium===4.18.1
webdriver_manager===4.0.1
werkzeug===2.2.2
waitress===3.0.0
google-auth-oauthlib===1.1.0
google-api-python-client===2.122.0
undetected_chromedriver===3.5.5
我转而使用未检测到的 chromedriver 以查看是否可以解决问题,但我遇到了同样的问题。
如果有人想提供帮助,请告诉我是否需要更多信息。谢谢!
不确定您的驱动程序脚本是什么样子,但假设像这样简单:
🗎
run.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--no-sandbox")
options.add_argument("--headless")
options.add_argument("--window-size=1920,1080")
print("Launch Chrome.")
driver = webdriver.Chrome(options=options)
print("Get www.example.com.")
driver.get("http://www.example.com")
print("Close Chrome.")
driver.quit()
🗎
Dockerfile
(无需将 Python 安装到 Debian 基础映像上,只需使用 Python 基础映像即可。)
FROM python:3.11.3
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y \
&& apt-get install -y \
wget \
gnupg \
unzip \
xz-utils \
build-essential \
zlib1g-dev \
libncurses5-dev \
libgdbm-dev \
libssl-dev \
libreadline-dev \
libffi-dev \
libsqlite3-dev \
libbz2-dev \
libnss3-dev \
openssl \
&& pip3 install --upgrade pip
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list \
&& apt-get update && apt-get install -y \
google-chrome-stable
RUN rm -rf /var/lib/apt/lists/*
COPY requirements.txt /tmp/
RUN pip3 install -r /tmp/requirements.txt
COPY run.py .
CMD python3 run.py
🗎
requirements.txt
(精简为仅包含软件包以获得工作示例。)
selenium===4.18.1