Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running ChromeDriver with Python selenium on Heroku

So I have a Flask server on Heroku which has been working fine as expected for some time.Now, as per new requirements, I need to add functionality to the Flask server to fetch a page from an external website.Because of reasons best known to me, I am using Selenium along with Chrome web driver to do this.Locally I was able to set this up and it works fine but I am quite unsure as to how to set it up on the Heroku server. I read a bit about buildpacks and found this buildpack for ChromeDriver :

https://elements.heroku.com/buildpacks/jimmynguyc/heroku-buildpack-chromedriver

However, I am not sure how to proceed further.How do I install chromium browser itself and what else is needed to tie it all up ?

like image 813
Amistad Avatar asked Dec 09 '16 11:12

Amistad


1 Answers

I had the same issue and the following steps worked fine for me:

  • I added the following buildpacks on heroku: https://github.com/heroku/heroku-buildpack-xvfb-google-chrome (to install chrome, since chromedriver requires it) and https://github.com/heroku/heroku-buildpack-chromedriver.
  • I created an environment variable GOOGLE_CHROME_BIN, with the path of chrome on heroku: /app/.apt/usr/bin/google-chrome and an environment variable called CHROMEDRIVER_PATH with the path of chromedriver on heroku: /app/.chromedriver/bin/chromedriver.
  • In my python file, I configured chromedriver:

    chrome_options = Options()
    chrome_options.binary_location = GOOGLE_CHROME_BIN
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--no-sandbox')
    driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, chrome_options=chrome_options)
    

(First, I tried to configure chromedriver with no arguments, but I faced the following error: "Chrome failed to start: crashed". --disable-gpu and --no-sandbox solved the problem for me).

like image 76
Emanoeli M. Avatar answered Sep 16 '22 23:09

Emanoeli M.