How To Run Selenium With Chrome In Docker
You need to launch a standalone chrome browser
docker run -d -p 4444:4444 selenium/standalone-chrome
and then in your python script launch browser using Remote webdriver
from selenium import webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesdriver = webdriver.Remote("http://127.0.0.1:4444/wd/hub", DesiredCapabilities.CHROME)
If you want you can also launch a Selenium Grid hub.
To do this as a django test do the following:
# docker-compse.ymlselenium: image: selenium/standalone-firefox ports: - 4444:4444# project/app/test.pyfrom django.test import TestCasefrom selenium import webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesclass 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')
Note:
If you use webdriver.ChromeOptions|FirefoxOptions|etc
then DesiredCapabalities
import is not necessary:
from selenium import webdriveroptions = webdriver.ChromeOptions()options.add_argument('--headless') # exampledriver = webdriver.Remote("http://127.0.0.1:4444/wd/hub", options=options)
You need to add the next lines to your Dockerfile:
# install google chromeRUN 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 updateRUN apt-get install -y google-chrome-stable# install chromedriverRUN apt-get install -yqq unzipRUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zipRUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/# set display port to avoid crashENV DISPLAY=:99# install seleniumRUN pip install selenium==3.8.0
Then your code should be like this. Especially you need to declare your driver
like below:
from selenium import webdriverchrome_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()
For people coming through google search, here is the easiest workaround:
Dockerfile
FROM python:3.7RUN 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 chromeRUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.debRUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install#install python dependenciesCOPY requirements.txt requirements.txt RUN pip install -r ./requirements.txt #some envsENV APP_HOME /app ENV PORT 5000#set workspaceWORKDIR ${APP_HOME}#copy local filesCOPY . . 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.txt
as:
Flask==1.1.1gunicorn==20.0.4selenium==3.141.0chromedriver-binary==79.0.3945.36
Then your main.py
should be like:
from selenium import webdriverimport chromedriver_binarychrome_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 Dockerfile
using docker build -t <imagename> .
and docker run --rm -p <yourPORT>:5000 <imagename>