close

How To Run Selenium With Chrome In Docker

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How To Run Selenium With Chrome In Docker in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How To Run Selenium With Chrome In Docker?

  1. 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

  2. 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

Method 1

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 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.

To do this as a django test do the following:

# 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')

Method 2

You need to add the next lines to your 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 driver like 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()

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read