Running ChromeDriver with Python Selenium on Heroku
While building a personal project to scrape data, I wanted to be able to deploy the project as an app. Heroku seemed to be straight forward… until it wasn’t.
It turns out that with Heroku, you have to install “Build packages” to get the chrome drivers for Selenium. Then you have to add those build packages to the PATH variables in Heroku. The overall solution came from multiple StackOverflow partial solutions:
- https://stackoverflow.com/questions/42860703/how-to-use-selenium-webdriver-on-heroku
- https://stackoverflow.com/questions/41059144/running-chromedriver-with-python-selenium-on-heroku
- https://stackoverflow.com/questions/46374486/creating-an-environment-variable-with-the-path-of-chrome
I wanted to create this post to merge the solutions together into a unified, easy to follow solution for anyone who needs it.
Assumptions:
- The general guidelines for setting up an app and deploying to Heroku have been met and the app is technically created.
- You are working in Windows; Note I am also using the PyCharm IDE.
Step 1: Download the Buildpacks Necessary for the ChromeDriver
In Heroku, there are two build packs needed to get the ChromeDriver to work properly. To “add” them input the following into the terminal:
heroku buildpacks:add --index 1 https://github.com/heroku-buildpack-chromedriverheroku buildpacks:add --index 2 https://github.com/heroku-buildpack-chromedriver
Step 2: Add the PATH variable to the Heroku configuration
Again, in the terminal:
heroku config:set GOOGLE_CHROME_BIN=/app/.apt/usr/bin/google_chromeheroku config:set CHROMEDRIVER_PATH=/app/.chromedriver/bin/chromedriver
Step 3: The Code
In the Python file that you will need to build the browser:
Imports:
from selenium import webdriver
Assign path variables:
GOOGLE_CHROME_PATH = '/app/.apt/usr/bin/google_chrome'
CHROMEDRIVER_PATH = '/app/.chromedriver/bin/chromedriver'
Set the Chrome Options:
chrome_options = webdriver.ChromeOptions()chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')chrome_options.binary_location = GOOGLE_CHROME_PATH
Build the browser:
browser = webdriver.Chrome(execution_path=CHROMEDRIVER_PATH, chrome_options=chrome_options)
Summary
If you have any questions on specifics, feel free to comment. There isn’t too much for clear and concise documentation for this specific problem so I am happy to help out.