Скачать пример файлом - example_chromedriver.zip
Структура папок и файлов:
wrapper/Dockerfile
wrapper/requirements.txt
wrapper/main.py
docker-compose-yml
Содержимое docker-compose.yml
:
version: "2"
services:
web:
build: wrapper
restart: always
command: python main.py
Содержимое wrapper/Dockerfile
:
FROM python:3.6
ENV PYTHONUNBUFFERED 1
# 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
RUN mkdir /code
WORKDIR /code
COPY . /code/
RUN pip install -r requirements.txt
Содержимое wrapper/requirements.txt
:
selenium==3.8.0
Содержимое wrapper/main.py
:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.implicitly_wait(10)
while True:
print('start')
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
print('elem', elem)
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
print('all ok')
time.sleep(3)
driver.close()
Источник: https://github.com/joyzoursky/docker-python-chromedriver