A step-by-step guide on how to install selenium and chromedriver on Linux OS to run headless scraping

Photo by Nicolas Picard on Unsplash
Photo by Nicolas Picard on Unsplash

Selenium is a popular framework for testing web applications. You’re probably here because you have written and tested your web scraping/testing script on your local machine. Now, you’re wondering…

How can I automatically run my Selenium script every X hours/day/week?

Therefore, this guide will show you how you can set up a Linux Virtual Machine (VM) with Selenium and Chromedriver to do exactly that. Although you can always use a Windows OS but let’s focus on a Linux OS for this guide.

Installation

To get selenium and Chromedriver running on your local machine, it can be broken down into 3 simple steps:

  1. Install dependencies
  2. Install Chrome binary and Chromedriver
  3. Install Selenium

Step 1

Whenever you get a new Linux machine, always update the packages first. Then install the necessary dependencies.

sudo apt-get update
sudo apt-get install -y unzip openjdk-8-jre-headless xvfb libxi6 libgconf-2-4

Step 2

In order for Chromedriver to work on Linux, you’ll have to install Chrome binary.

# Install Chrome.
sudo curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add
sudo echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
sudo apt-get -y update
sudo apt-get -y install google-chrome-stable

For Chromedriver version, it’ll be dependent on your Chrome binary version.

# Install ChromeDriver.
wget -N https://chromedriver.storage.googleapis.com/79.0.3945.36/chromedriver_linux64.zip -P ~/
unzip ~/chromedriver_linux64.zip -d ~/
rm ~/chromedriver_linux64.zip
sudo mv -f ~/chromedriver /usr/local/bin/chromedriver
sudo chown root:root /usr/local/bin/chromedriver
sudo chmod 0755 /usr/local/bin/chromedriver

Step 3

Last but not least, install Selenium.

# Install Selenium
pip install selenium

And… you’re all set!

Testing your script

All that is left to do is to check if Selenium and Chromedriver are installed correctly and you’re able to run a python script that uses Selenium.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

CHROMEDRIVER_PATH = '/usr/local/bin/chromedriver'
WINDOW_SIZE = "1920,1080"

chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
chrome_options.add_argument('--no-sandbox')

driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH,
                            chrome_options=chrome_options
                            )
driver.get("[https://www.google.com](https://www.google.com)")
print(driver.title)
driver.close()

If it prints “Google”, it means you have successfully run Selenium with Chromedriver on a Linux VM!

Bonus

If you are looking for a DockerFile, you’re in luck. Hope this helps!