How to setup Selenium on a Linux VM

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

Zhun Hung
Towards Data Science

--

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.

Step 2

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

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

Step 3

Last but not least, 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")
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!

--

--