Aur*_*tas 2 python selenium-chromedriver docker google-cloud-platform dockerfile
我已经度过了最后两天,我正在失去我的头发。我在我的 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/
Run Code Online (Sandbox Code Playgroud)
我写 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)
Run Code Online (Sandbox Code Playgroud)
我收到
selenium.common.exceptions.WebDriverException: Message: 'chromedriver_linux' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
有趣的是,如果我在本地机器上运行 Docker 并为 mac 指定 chromedriver - 它可以工作。我无法弄清楚 - 为什么它可以在本地机器上运行但不能在云上运行。
期待听到你的声音,聪明的人,我在这里错过了什么!
终于在几个非睡眠日之后我得到了它。
首先,我运行的是 Ubuntu 的 Alpine 版本。这是第一个问题。但我让它工作了。所以如果你们也在运行 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
Run Code Online (Sandbox Code Playgroud)
整个魔术就是安装 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')
Run Code Online (Sandbox Code Playgroud)
奖金。我想使用不同的,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
Run Code Online (Sandbox Code Playgroud)
我们的代码如下所示:
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()
Run Code Online (Sandbox Code Playgroud)
我希望它可以为您节省数小时。快乐编码!
| 归档时间: |
|
| 查看次数: |
1135 次 |
| 最近记录: |