无法在Docker内运行Selenium

5w9g7ksd  于 2022-12-18  发布在  Docker
关注(0)|答案(1)|浏览(180)

请帮助我运行 selenium 内Docker。下面是我的代码,这是不工作。我添加错误信息以及。

停靠文件:

FROM python:3.8-slim

# Install the Selenium Python bindings
RUN pip install selenium

# Download the ChromeDriver executable and add it to the PATH
RUN wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip \
    && unzip /tmp/chromedriver.zip -d /usr/local/bin \
    && rm /tmp/chromedriver.zip
ENV PATH="/usr/local/bin:${PATH}"

停靠-合成:

version: '3'
services:
  selenium:
    image: selenium/standalone-chrome
    ports:
      - "4444:4444"
      - "5900:5900"
  webapp:
    build: .
    volumes:
      - .:/app
    command: python3 test.py
    depends_on:
      - selenium

test.py代码片段:

from selenium import webdriver

driver = webdriver.Remote(
    command_executor='http://selenium:4444/wd/hub',
    desired_capabilities={'browserName': 'chrome'}
)

driver.get('https://www.google.com')
driver.save_screenshot('search_results.png')
driver.quit()

运行命令:

docker-compose up

错误堆栈跟踪:

ERROR [3/3] RUN wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip     0.8s 
------
 > [3/3] RUN wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip     && unzip /tm
p/chromedriver.zip -d /usr/local/bin     && rm /tmp/chromedriver.zip:
#0 0.736 /bin/sh: 1: wget: not found
------
failed to solve: executor failed running [/bin/sh -c wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/c
hromedriver_linux64.zip     && unzip /tmp/chromedriver.zip -d /usr/local/bin     && rm /tmp/chromedriver.zip]: exit code: 127

**其他说明:**我正在特灵在本地计算机上运行它,而我的计算机是Windows 10。

请注意所有的文件都在一个主目录下。这里有什么问题吗?请帮我修正一下。

更新停靠文件:

FROM python:3.8-slim

# Install the Selenium Python bindings
RUN pip install selenium

RUN apt update && apt install -y wget

# Download the ChromeDriver executable and add it to the PATH
RUN wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip \
    && unzip /tmp/chromedriver.zip -d /usr/local/bin \
    && rm /tmp/chromedriver.zip
ENV PATH="/usr/local/bin:${PATH}"

新错误消息:

#0 2.034 2022-12-17 08:19:54 (6.27 MB/s) - ‘/tmp/chromedriver.zip’ saved [5404417/5404417]
#0 2.034
#0 2.037 /bin/sh: 1: unzip: not found
------
failed to solve: executor failed running [/bin/sh -c wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/c
hromedriver_linux64.zip     && unzip /tmp/chromedriver.zip -d /usr/local/bin     && rm /tmp/chromedriver.zip]: exit code: 127
whlutmcx

whlutmcx1#

您应该在RUN pip install selenium后面添加:

apt update && apt install -y wget zip

相关问题