在 google 云 运行 应用程序中为 python selenium 安装 google chrome
Install google chrome for python selenium in a google cloud run app
我正在尝试在我正在开发的云 运行 应用程序中使用 selenium。该应用程序在我的本地计算机上正常运行,但在我将其部署到 google 云 运行 上后无法运行。我收到的错误与未在 google 云上安装 chromedriver.exe
有关。
这是日志中的回溯错误:
Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise raise value File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/app/main.py", line 22, in daily_login login.create_access_token() File "/app/scripts/loginFlow/login.py", line 30, in create_access_token request_token = generate_request_token() File "/app/scripts/loginFlow/login.py", line 61, in generate_request_token driver = webdriver.Chrome() File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__ self.service.start() File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
该回溯的相关部分似乎是 Message: 'chromedriver' executable needs to be in PATH
。我如何在这个 google 云 运行 应用程序的 PATH 中获取 chromedriver 可执行文件?
现在,我尝试过做什么?
首先,我找到了这个 post: unable to run selenium chrome-driver on google-cloud-run and followed the instructions left in the comments. I looked at this post on dev.to 并按照建议的方式对我的 Dockerfile
进行了建模。唯一的区别是我使用 Python3.6 而不是 Python3.7:
# Use the official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.6-slim
# Install manually all the missing libraries
RUN apt-get update
RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
# Install Chrome
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install
# Install Python dependencies.
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./
# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app
我还在 requirements.txt
文件中添加了 chromedriver-binary==77.0.3865.40.0
。
当我尝试使用 gcloud builds submit
部署它时,出现此错误:
/bin/sh: 1: wget: not found
The command '/bin/sh -c wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb' returned a non-zero code: 127
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 127
我一直在尝试在线寻找此错误的解决方案,但我迷路了。非常感谢您的帮助!
添加安装命令wget
。
# Install manually all the missing libraries
RUN apt-get update
RUN apt-get install wget
我遇到了同样的问题,终于搞定了!这是我的 Dockfile:
FROM python:3.7
# Install manually all the missing libraries
RUN apt-get update
RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils default-jdk
# Install Chrome
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install
# Install Python dependencies.
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
# Copy local code to the container image.
WORKDIR /app
COPY . .
CMD gunicorn --bind :$PORT --workers 1 --threads 3 main:app --timeout 90
我正在尝试在我正在开发的云 运行 应用程序中使用 selenium。该应用程序在我的本地计算机上正常运行,但在我将其部署到 google 云 运行 上后无法运行。我收到的错误与未在 google 云上安装 chromedriver.exe
有关。
这是日志中的回溯错误:
Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise raise value File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/app/main.py", line 22, in daily_login login.create_access_token() File "/app/scripts/loginFlow/login.py", line 30, in create_access_token request_token = generate_request_token() File "/app/scripts/loginFlow/login.py", line 61, in generate_request_token driver = webdriver.Chrome() File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__ self.service.start() File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
该回溯的相关部分似乎是 Message: 'chromedriver' executable needs to be in PATH
。我如何在这个 google 云 运行 应用程序的 PATH 中获取 chromedriver 可执行文件?
现在,我尝试过做什么?
首先,我找到了这个 post: unable to run selenium chrome-driver on google-cloud-run and followed the instructions left in the comments. I looked at this post on dev.to 并按照建议的方式对我的 Dockerfile
进行了建模。唯一的区别是我使用 Python3.6 而不是 Python3.7:
# Use the official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.6-slim
# Install manually all the missing libraries
RUN apt-get update
RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
# Install Chrome
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install
# Install Python dependencies.
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./
# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app
我还在 requirements.txt
文件中添加了 chromedriver-binary==77.0.3865.40.0
。
当我尝试使用 gcloud builds submit
部署它时,出现此错误:
/bin/sh: 1: wget: not found
The command '/bin/sh -c wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb' returned a non-zero code: 127
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 127
我一直在尝试在线寻找此错误的解决方案,但我迷路了。非常感谢您的帮助!
添加安装命令wget
。
# Install manually all the missing libraries
RUN apt-get update
RUN apt-get install wget
我遇到了同样的问题,终于搞定了!这是我的 Dockfile:
FROM python:3.7
# Install manually all the missing libraries
RUN apt-get update
RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils default-jdk
# Install Chrome
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install
# Install Python dependencies.
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
# Copy local code to the container image.
WORKDIR /app
COPY . .
CMD gunicorn --bind :$PORT --workers 1 --threads 3 main:app --timeout 90