为 selenium 创建带有 GUI 的 docker
2023-07-26
647
我有一个程序,它从数据库获取信息,访问网站并执行一些命令。我正在使用 selenium,我需要在非无头模式下运行它
让我们切开程序:
import time
from selenium import webdriver
from selenium_stealth import stealth
options = webdriver.ChromeOptions()
chrome_driver_path = "chromedriver.exe"
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options)
stealth(driver,
languages=["en-US", "en"],
vendor="Google Inc.",
platform="Win32",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
)
driver.get('https://google.com')
time.sleep(10)
driver.close()
driver.quit()
print('Test done')
我需要在 dockerfile 中写入什么来运行它?
这是我的尝试: I.
dockerfile:
FROM python:3.9
RUN apt-get update \
&& apt-get install -y wget unzip gnupg2
RUN wget -q -O - https://dl.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 CHROME_DRIVER_VERSION=$(wget -q -O - https://chromedriver.storage.googleapis.com/LATEST_RELEASE) \
&& wget -q https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip \
&& unzip chromedriver_linux64.zip \
&& mv chromedriver /usr/local/bin \
&& chmod +x /usr/local/bin/chromedriver \
&& rm chromedriver_linux64.zip
WORKDIR /app
COPY main.py requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
CMD ["python", "main.py"]
错误:
2023-07-26 12:39:59 Traceback (most recent call last):
2023-07-26 12:39:59 File "/app/main.py", line 11, in <module>
2023-07-26 12:39:59 driver = webdriver.Chrome(options=options)
2023-07-26 12:39:59 File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 49, in __init__
2023-07-26 12:39:59 super().__init__(
2023-07-26 12:39:59 File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/chromium/webdriver.py", line 54, in __init__
2023-07-26 12:39:59 super().__init__(
2023-07-26 12:39:59 File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 206, in __init__
2023-07-26 12:39:59 self.start_session(capabilities)
2023-07-26 12:39:59 File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 291, in start_session
2023-07-26 12:39:59 response = self.execute(Command.NEW_SESSION, caps)["value"]
2023-07-26 12:39:59 File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
2023-07-26 12:39:59 self.error_handler.check_response(response)
2023-07-26 12:39:59 File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
2023-07-26 12:39:59 raise exception_class(message, screen, stacktrace)
2023-07-26 12:39:59 selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
2023-07-26 12:39:59 (unknown error: DevToolsActivePort file doesn't exist)
2023-07-26 12:39:59 (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
2023-07-26 12:39:59 Stacktrace:
2023-07-26 12:39:59 #0 0x55cb45ef94e3 <unknown>
2023-07-26 12:39:59 #1 0x55cb45c28c76 <unknown>
2023-07-26 12:39:59 #2 0x55cb45c51d78 <unknown>
2023-07-26 12:39:59 #3 0x55cb45c4e029 <unknown>
2023-07-26 12:39:59 #4 0x55cb45c8cccc <unknown>
2023-07-26 12:39:59 #5 0x55cb45c8c47f <unknown>
2023-07-26 12:39:59 #6 0x55cb45c83de3 <unknown>
2023-07-26 12:39:59 #7 0x55cb45c592dd <unknown>
2023-07-26 12:39:59 #8 0x55cb45c5a34e <unknown>
2023-07-26 12:39:59 #9 0x55cb45eb93e4 <unknown>
2023-07-26 12:39:59 #10 0x55cb45ebd3d7 <unknown>
2023-07-26 12:39:59 #11 0x55cb45ec7b20 <unknown>
2023-07-26 12:39:59 #12 0x55cb45ebe023 <unknown>
2023-07-26 12:39:59 #13 0x55cb45e8c1aa <unknown>
2023-07-26 12:39:59 #14 0x55cb45ee26b8 <unknown>
2023-07-26 12:39:59 #15 0x55cb45ee2847 <unknown>
2023-07-26 12:39:59 #16 0x55cb45ef2243 <unknown>
2023-07-26 12:39:59 #17 0x7f6861704fd4 <unknown>
2023-07-26 12:39:59
II.
Dockerfile:
# Use an official Python runtime as a base image
FROM python:3.9-slim-buster
# Install system dependencies
RUN apt-get update \
&& apt-get install -y wget unzip gnupg2
# Install Chrome
RUN wget -q -O - https://dl.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 ChromeDriver
RUN CHROME_DRIVER_VERSION=$(wget -q -O - https://chromedriver.storage.googleapis.com/LATEST_RELEASE) \
&& wget -q https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip \
&& unzip chromedriver_linux64.zip \
&& mv chromedriver /usr/local/bin \
&& chmod +x /usr/local/bin/chromedriver \
&& rm chromedriver_linux64.zip
# Additional configuration to allow Chrome to run in headless mode
ENV DISPLAY=:99
# Set up the XVFB server for running Chrome in headless mode
RUN apt-get install -y xvfb
RUN Xvfb :99 -screen 0 1024x768x24 -ac +extension GLX +render -noreset &
# Set the working directory in the container
WORKDIR /app
# Copy the Python script and requirements file to the container
COPY main.py requirements.txt ./
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Run the Python script
CMD ["xvfb-run", "python", "main.py"]
它什么也没做
2个回答
我找到了解决方案,我所需要的只是设置新版本的无头模式
chrome_options.add_argument('--headless=new')
Nero
2023-09-13
您的 Docker 文件似乎存在一些问题。
我对您的 Dockerfile 进行了一些更改,它可能会起作用:
# Use an official Python runtime as a base image
FROM python:3.9-slim-buster
# Install system dependencies
RUN apt-get update && apt-get install -y wget unzip gnupg2
# Install Chrome and additional required libraries
RUN wget -q -O - https://dl.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 \
fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf \xvfb
# Install ChromeDriver
RUN CHROME_DRIVER_VERSION=$(wget -q -O - https://chromedriver.storage.googleapis.com/LATEST_RELEASE) \
&& wget -q https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip \
&& unzip chromedriver_linux64.zip \
&& mv chromedriver /usr/local/bin \
&& chmod +x /usr/local/bin/chromedriver \
&& rm chromedriver_linux64.zip
# Set up the XVFB server for running Chrome in headless mode
ENV DISPLAY=:99
RUN Xvfb :99 -screen 0 1024x768x24 -ac +extension GLX +render -noreset &
# Set the working directory in the container
WORKDIR /app
# Copy the Python script and requirements file to the container
COPY main.py requirements.txt ./
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Run the Python script
CMD ["python", "main.py"]
Dockerfile 中所做的更改:
-
我将其他必需库(字体)的安装与 Chrome 的安装结合起来,以尽量减少单独的 apt-get 命令的数量。
-
从 CMD 中删除了 xvfb-run 命令,因为我们现在正在运行 Xvfb 服务器作为 Dockerfile 的一部分。
-
我们设置了 ENV DISPLAY=:99 来设置在无头模式下运行 Chrome 所需的显示环境变量。
-
Xvfb 服务器以 Xvfb :99 ... 启动并在后台运行。
-
CMD ["python", "main.py"] 保持不变,因为这是运行 Python 脚本的 命令。
我希望通过这些更改,您的 Docker 容器现在可以在无头模式下运行。
啊!此外,请确保您的 ' main.py ' 脚本和 ' requirements.txt ' 与 Dockerfile 位于同一目录中。
您可以查看 此 以获取进一步参考。
Yes_ItsMEOW
2023-07-26