Python 如何在 Docker 中使用 Chrome 运行 Selenium

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45323271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:55:14  来源:igfitidea点击:

How To Run Selenium With Chrome In Docker

pythonpython-2.7seleniumdockergoogle-chrome

提问by J. Fan

I installed google-chrome in a Docker, but when I run my Python 2script of Selenium, it failed like this:

我在Docker 中安装了 google-chrome ,但是当我运行Selenium 的Python 2脚本时,它失败了,如下所示:

automation@1c17781fef0c:/topology-editor/test$ python test.py
Traceback (most recent call last):
  File "test.py", line 27, in <module>
    browser = webdriver.Chrome()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 98, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 185, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 249, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Linux 4.4.0-83-generic x86_64)

And if I run google-chrome directly in docker, it shows below:

如果我直接在 docker 中运行 google-chrome,它会显示如下:

automation@1c17781fef0c:/topology-editor/test$ google-chrome
Failed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Operation not permitted
Trace/breakpoint trap (core dumped)
automation@1c17781fef0c:/topology-editor/test$

System:

系统:

$ uname -a
Linux 1c17781fef0c 4.4.0-83-generic #106-Ubuntu SMP Mon Jun 26 17:54:43 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
$ google-chrome --version
Google Chrome 60.0.3112.78
$ chromedriver --version
ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8)

回答by Tarun Lalwani

You need to launch a standalone chrome browser

您需要启动一个独立的 chrome 浏览器

docker run -d -p 4444:4444 selenium/standalone-chrome

and then in your python script launch browser using Remote webdriver

然后在你的 python 脚本中使用 Remote webdriver 启动浏览器

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote("http://127.0.0.1:4444/wd/hub", DesiredCapabilities.CHROME)

If you want you can also launch a Selenium Grid hub.

如果需要,您还可以启动 Selenium Grid 集线器。

To do this as a django test do the following:

要将此作为 django 测试执行以下操作:

# docker-compse.yml

selenium:
  image: selenium/standalone-firefox
  ports:
  - 4444:4444

# project/app/test.py

from django.test import TestCase
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


class SiteTest(TestCase):
    fixtures = [
        'app/fixtures/app.json',
        ...
    ]

    def setUp(self):
        self.browser = webdriver.Remote("http://selenium:4444/wd/hub", DesiredCapabilities.FIREFOX)

    def tearDown(self):
        self.browser.quit()

    def test_visit_site(self):
        self.browser.get('http://app:8000/')
        self.assertIn(self.browser.title, 'Home')

回答by Mehmet nuri

The best way to use selenium in a docker container you need to do followings.

在 docker 容器中使用 selenium 的最佳方法,您需要执行以下操作。

Your need to add next lines to your Dockerfile

您需要在Dockerfile 中添加下一行

# install google chrome
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
RUN apt-get -y update
RUN apt-get install -y google-chrome-stable

# install chromedriver
RUN apt-get install -yqq unzip
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/

# set display port to avoid crash
ENV DISPLAY=:99

# install selenium
RUN pip install selenium==3.8.0

Then your code should be like this. Especially you need to declare your driverlike below:

那么你的代码应该是这样的。特别是你需要像下面这样声明你的驱动程序

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--window-size=1420,1080')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)

driver.get('www.google.com')
screenshot = driver.save_screenshot('test.png')
driver.quit()

回答by anisbhsl

For people coming through google search, here is the easiest workaround:

对于通过谷歌搜索来的人,这是最简单的解决方法:

Dockerfile

Dockerfile

FROM python:3.7

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

#download and 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 

#some envs
ENV APP_HOME /app 
ENV PORT 5000

#set workspace
WORKDIR ${APP_HOME}

#copy local files
COPY . . 

CMD exec gunicorn --bind :${PORT} --workers 1 --threads 8 main:app 

You will need to install chromedriver_binary pip package which I have added in requirements.txtas:

您将需要安装我添加的 chromedriver_binary pip 包requirements.txt

Flask==1.1.1
gunicorn==20.0.4
selenium==3.141.0
chromedriver-binary==79.0.3945.36

Then your main.pyshould be like:

那么你main.py应该是这样的:

from selenium import webdriver
import chromedriver_binary


chrome_options=webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("window-size=1400,2100") 
chrome_options.add_argument('--disable-gpu')

driver=webdriver.Chrome(chrome_options=chrome_options)

Now, build your Dockerfileusing docker build -t <imagename> .and docker run --rm -p <yourPORT>:5000 <imagename>

现在,构建您的Dockerfile使用docker build -t <imagename> .docker run --rm -p <yourPORT>:5000 <imagename>

回答by skaliber

I assume you need to run it in headless mode

我假设您需要在无头模式下运行它

https://developers.google.com/web/updates/2017/04/headless-chrome

https://developers.google.com/web/updates/2017/04/headless-chrome

this is how I do it in ruby

这就是我在 ruby​​ 中的做法

capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: {args: %w[ no-sandbox headless disable-gpu window-size=1280,1000 ]})

Capybara::Selenium::Driver.new(app, :browser => :chrome, http_client: client, desired_capabilities: capabilities)

You can pretty much adapt your code in python

你几乎可以在 python 中调整你的代码

also consider installing some font libriaries that chrome needs to run on headless

还可以考虑安装一些 chrome 需要在 headless 上运行的字体库

RUN apt-get update && \
    apt-get -qq -y install  libxpm4 libxrender1 libgtk2.0-0 libnss3\ 
       libgconf-2-4  libpango1.0-0 libxss1 libxtst6 fonts-liberation\ 
       libappindicator1 xdg-utils

RUN apt-get -y install \
               xvfb gtk2-engines-pixbuf \
               xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable \
               imagemagick x11-apps zip