Running ChromeDriver with Python Selenium on Heroku

My Video Tutorial

I have recently been experimenting with the Heroku platform for building basic web apps, and my experience has been nothing but excellent so far.

At least, that was until I tried to incorporate Selenium into one of my Python projects.

Selenium is a Python library that can be used to “drive” web browsers such as Chrome, Firefox, etc so you can access websites, perform web scraping, and build tools to test your web apps. The problem here was that my code for Selenium refused to execute on Heroku. I wasn’t able to find any information about this issue readily available online, so when I finally solved the issue, I went ahead and created the above video tutorial. Several people have commented that this method fixed their problems, so I’ll post a text version of this tutorial as well.

Step 1: Set Up Your Code

Add the following lines to your code before you use Selenium.

from selenium import webdriver
import os

chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), chrome_options=chrome_options)

# Now you can start using Selenium

Step 2: Add the Buildpacks

On Heroku, open your App. Click on the Settings tab and scroll down to Buildpacks. Add the following:

Step 3: Add the Config Vars

Scroll to the config vars section. Here, we will add the paths to Chrome and the Chromedriver. Add the following config vars:

  • CHROMEDRIVER_PATH = /app/.chromedriver/bin/chromedriver
  • GOOGLE_CHROME_BIN = /app/.apt/usr/bin/google-chrome

Step 4: Deploy the Application

If everything worked out correctly, then your application should be ready to deploy!