• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

seanpianka/docker-python-xvfb-selenium-chrome-firefox: Dockerfiles for Python + ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

seanpianka/docker-python-xvfb-selenium-chrome-firefox

开源软件地址:

https://github.com/seanpianka/docker-python-xvfb-selenium-chrome-firefox

开源编程语言:

Dockerfile 73.0%

开源软件介绍:

docker-python-xvfb-chromium-selenium

Dockerfiles for images running Python 2.7 or Python 3.6 + Selenium with either Chrome or Firefox and using Xvfb for the X display (necessary for running Selenium headlessly).

Dependencies

  • Python 2.7 or Python 3.6
  • Google Chrome/Chromium or Firefox (Unstable, from Debian)
  • Chromedriver or Geckodriver
  • Selenium
  • Xvfb

Usage Tutorial

Your Project Files

A project directory under /opt/app/ is created within the Docker image. This is where all of your project files will be copied over (/opt/app/ is a common convention). Create a new file (this will be our project file for this tutorial) using $ touch test.py. We'll write our application in this file, see below:

from selenium import webdriver

# use selenium, instantiate Chrome browser instance
options = webdriver.chrome.options.Options()
options.add_argument("--no-sandbox")
options.add_argument("--disable-setuid-sandbox")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options)

# perform a test request to verify that selenium and chrome are working properly
driver.get("https://httpstat.us/200")
if "200 OK" in driver.page_source:
    print('Selenium successfully opened with Chrome (under the Xvfb display) and navigated to "https://httpstat.us/200", you\'re all set!')
Your Project's Entry Point

Then, modify the invocation shell script run.sh (AVAILABLE HERE) and ensure the last line contains the correct Python invocation of the entry point into your application. This script is what the Dockerfile will use, along with Bash, to invoke the Python interpreter on the entry point you specify.

python3 test.py  # change this line to invoke the entry point of your application!
Dockerfile Explanation and Build Steps

Now, we're going to build and run our project! Yay! Start by modifying the Dockerfile (AVAILABLE HERE) for Python 3 + Chrome.

First, the Dockerfile installs chromium, Xvfb, and Python. Then, a project directory is created, the Python project dependencies (and Selenium) are installed, and your project code is copied into the image. Lastly, env DISPLAY is set to an open display port for Xvfb to use (as we require an X Windows server), your invocation script run.sh (AVAILABLE HERE) is copied into the image and invoked with /bin/bash.

Now, build the image and spawn a new container using that image:

$ docker build -t docker-python-xvfb-selenium-chrome-firefox .
Executing Your Project

Now, create a new container from your image:

$ docker run --rm docker-python-xvfb-selenium-chrome-firefox

You should see the following text output to the terminal:

Selenium successfully opened with Chrome (under the Xvfb display) and navigated to "https://httpstat.us/200", you're all set!

Done!

Pre-written Dockerfiles for any combination of Python 2/Python 3 and Chrome/Firefox are available!




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap