gpt4 book ai didi

python - Docker + Google Cloud + chromedriver -> 可执行文件需要位于 PATH 中

转载 作者:行者123 更新时间:2023-11-30 21:55:09 37 4
gpt4 key购买 nike

我已经花了两天时间脱发了。我在我的 Google Cloud 计算机上运行 ubuntu。我的 Dockerfile 看起来像这样


# Project files
ARG PROJECT_DIR=/srv/api
RUN mkdir -p $PROJECT_DIR
WORKDIR $PROJECT_DIR

# Install Python dependencies
COPY ./ ./
RUN mv /srv/api/app/chromedriver_linux /usr/bin/chromedriver_linux
RUN ls /usr/bin/

我编写 ls 来检查 chromedriver_linux 是否存在于我的路径中。它确实存在于 /usr/bin/chromedriver_linux

然后在我的代码中指定

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable_infobars')
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome('/usr/bin/chromedriver_linux', options=chrome_options)

我收到了

selenium.common.exceptions.WebDriverException:消息:“chromedriver_linux”可执行文件需要位于 PATH 中。请参阅 https://sites.google.com/a/chromium.org/chromedriver/home

有趣的是,如果我在本地计算机上运行 Docker 并为 mac 指定 chromedriver - 它就可以工作。我无法弄清楚 - 为什么它可以在本地计算机上运行但不能在云上运行。

期待听到你的声音,聪明人,我在这里错过了什么!

最佳答案

经过几天不 sleep 的日子,我终于明白了。

首先,我运行的是 Alpine 版本的 Ubuntu。这是第一个问题。但我成功了。因此,如果你们也运行 Alpine,这就是解决方案:

Dockerfile

FROM python:3.6.6-alpine3.8

# Project files
ARG PROJECT_DIR=/srv
RUN mkdir -p $PROJECT_DIR
WORKDIR $PROJECT_DIR

# Install Python dependencies
COPY ./ ./

RUN apk update
RUN apk add curl
RUN apk add unzip nano bash chromium chromium-chromedriver

RUN pip3 install -r requirements.txt

整个魔法就是安装 Chromium。现在我们的 *.py 看起来像这样:

mobile_emulation = {"deviceName": "iPhone X"}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

#chrome_options.add_argument('--disable_infobars')
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', options=chrome_options)
driver.get('https://google.com')

奖金。我想使用不同的 chromedriver 并认为 Alpine 确实把事情搞砸了。使用 python 安装了正确的 ubuntu 并使 chromedriver 工作。它看起来是这样的:Dockerfile

FROM ubuntu:18.04

# Project files
ARG PROJECT_DIR=/srv
RUN mkdir -p $PROJECT_DIR
WORKDIR $PROJECT_DIR


# Update
RUN apt-get update
RUN apt-get -y upgrade


# Set the locale
RUN apt-get install -y locales && locale-gen "en_US.UTF-8" && dpkg-reconfigure -f noninteractive locales
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
ENV PYTHONIOENCODING utf-8

RUN echo \
&& echo 'LANG=en_US.UTF-8' >> /etc/environment \
&& echo 'LANGUAGE=en_US:en' >> /etc/environment \
&& echo 'LC_ALL=en_US.UTF-8' >> /etc/environment \
&& echo 'PYTHONIOENCODING=utf-8' >> /etc/environment


# Install Python dependencies
RUN apt-get install --upgrade -y python3-pip
RUN apt-get install -y build-essential libssl-dev libffi-dev python3-dev
RUN apt-get install -y curl
RUN apt-get install -y unzip


# Copy everything to Docker
COPY ./ ./

# Install chromium instead
RUN apt-get install -y chromium-browser

# Install chromedriver for Chromium
RUN curl https://chromedriver.storage.googleapis.com/75.0.3770.140/chromedriver_linux64.zip -o /usr/local/bin/chromedriver.zip
RUN unzip /usr/local/bin/chromedriver.zip -d /usr/local/bin/
RUN chmod +x /usr/local/bin/chromedriver || rm /usr/local/bin/chromedriver.zip

RUN pip3 install -r requirements.txt

我们的代码如下所示:

 mobile_emulation = {"deviceName": "iPhone X"}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

#chrome_options.add_argument('--disable_infobars')
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver', options=chrome_options)
driver.get('https://google.com')
driver.close()

我希望它可以节省您的时间。快乐编码!

关于python - Docker + Google Cloud + chromedriver -> 可执行文件需要位于 PATH 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57243622/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com