【问题标题】:How do I write my Dockerfile to include chromedriver?如何编写我的 Dockerfile 以包含 chromedriver?
【发布时间】:2021-07-15 19:10:43
【问题描述】:

我是 Dockerfile 和 Selenium 的新手。我正在使用 selenium 进行网络抓取并截取屏幕截图。我正在尝试对其进行码头化。我的这个问题似乎在几个问题中得到了回答,但它并没有解决我的错误。仅供参考,我使用的是 Windows 笔记本电脑。

屏幕截图代码在我的本地机器上运行,但 dockerfile 似乎给了我错误。

我正在尝试使用这个版本的 chromedriver=89.0.4389.82

这是我更新后的 Dockefile,

FROM python:3.6

RUN pip install --upgrade pip && pip install pytest && pip install pytest-mock && pip install pytest-smtp && pip install mock \
pip install schedule && pip install selenium && pip install Selenium-Screenshot && pip install python-dateutil

# For running code
COPY src/screenshotcode.py /

RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
RUN apt-get update -y
RUN apt-get install -y google-chrome-stable
RUN apt-get install libxi6 libgconf-2-4 -y

ENV CHROMEDRIVER_VERSION 2.19
ENV CHROMEDRIVER_DIR /chromedriver
RUN mkdir -p $CHROMEDRIVER_DIR

# Download and install Chromedriver
RUN wget -q --continue -P $CHROMEDRIVER_DIR "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip"
RUN unzip $CHROMEDRIVER_DIR/chromedriver* -d $CHROMEDRIVER_DIR

# Put Chromedriver into the PATH
ENV PATH $CHROMEDRIVER_DIR:$PATH

CMD [ "python", "screenshotcode.py" ]

我的截图代码,

import time
from Screenshot import Screenshot_Clipping
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from email_it import email_it
from environmental_variables import environmental_variables
from error_alert_email import error_alert_email
from selenium import webdriver

def screenshot():

    ob=Screenshot_Clipping.Screenshot()
    
    chrome_options = Options()
    
    chrome_options.add_argument('--start-maximized')    
    chrome_options.add_argument('--start-fullscreen')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    
    driver = webdriver.Chrome(executable_path = r"C:\Users\me\Documents\Projects\chromedriver.exe")

    print('taking screenshot...')
    img_url=ob.full_Screenshot(driver, path = path, image_name = label)

    print('closing driver...')    
    driver.close()

screenshot()

编辑:我收到以下错误

PS C:\Users\me\Documents\Projects\> docker run screenshot
File "scheduler.py", line 16, in <module>
    from screenshot import screenshot
  File "/screenshotcode.py", line 72, in <module>
    screenshot()
  File "/screenshotcode.py", line 32, in screenshot
    driver = webdriver.Chrome()
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.19.346067 (6abd8652f8bc7a1d825962003ac88ec6a37a82f1),platform=Linux 5.4.72-microsoft-standard-WSL2 x86_64)

【问题讨论】:

    标签: python docker selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    您在代码中设置了 chromedriver 所在的位置:

    driver = webdriver.Chrome(executable_path = r"C:\Users\me\Documents\Projects\chromedriver.exe")
    

    但是在你的 dockerfile 中你有它在/usr/local/bin/chromedriver 所以您需要将代码更改为

    driver = webdriver.Chrome(executable_path = "/usr/local/bin/chromedriver")
    

    【讨论】:

    • 您提到的更改仍然存在错误。文件“/usr/local/lib/python3.6/subprocess.py”,第 1364 行,在 _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver ' selenium.common.exceptions.WebDriverException:消息:'chromedriver' 可执行文件需要在 PATH 中。请看sites.google.com/a/chromium.org/chromedriver/home
    • 如何在特定位置安装chromedriver??
    • 我看到你的更新,你只需要将你的chromedriver版本更新到89以匹配你的浏览器sites.google.com/chromium.org/driver
    • docker:来自守护进程的错误响应:OCI 运行时创建失败:container_linux.go:367:启动容器进程导致:exec:“python”:在 $PATH 中找不到可执行文件:未知。
    • 你是用ENV CHROMEDRIVER_VERSION 89.0.4389.23代替ENV CHROMEDRIVER_VERSION 2.19吗?
    猜你喜欢
    • 1970-01-01
    • 2020-05-24
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 2018-08-08
    • 2013-05-02
    相关资源
    最近更新 更多