用gui for selenium创建docker

y53ybaqx  于 2023-08-03  发布在  Docker
关注(0)|答案(1)|浏览(112)

我有一个程序,从数据库中获取信息,进入网站并执行一些命令。我用的是 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中写些什么来运行它?
这是我的尝试:一、
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


二、报告.
驳接文件:

# 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"]


但它什么也没做

mefy6pfw

mefy6pfw1#

您的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中所做的更改:
1.我将安装附加必需的库(字体)沿着安装Chrome相结合,以最大限度地减少单独的apt-get命令的数量。
1.从CMD中删除了xvfb-run命令,因为我们现在正在将Xvfb服务器作为Dockerfile的一部分运行。
1.我们设置ENV DISPLAY=:99来设置在无头模式下运行Chrome所需的显示环境变量。

  1. Xvfb服务器以Xvfb:99启动。并在后台运行。
  2. CMD [“python”,“main.py“]保持不变,因为这是运行Python脚本的命令。
    我希望通过这些更改,您的Docker容器现在可以在无头模式下运行了。
    啊!另外,确保你的'main.py'脚本和'requirements.txt'与Dockerfile在同一个目录下。
    您可以检查this以供进一步参考。

相关问题