Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set path to chromedriver in heroku chromedriver buildpack

I'm trying to set up selenium on heroku. I've been looking at Running ChromeDriver with Python selenium on Heroku for some help. Based on this I installed the 2 buildbacks listed. I'm using cedar-14 since the 16 stack is not supported.

When I run:

$ heroku buildpacks
===  Buildpack URLs
1. heroku/python
2. https://github.com/heroku/heroku-buildpack-chromedriver
3. https://github.com/heroku/heroku-buildpack-xvfb-google-chrome

In any case I'm trying to use

https://github.com/heroku/heroku-buildpack-chromedriver/tree/master/bin

My code contains:

options = webdriver.ChromeOptions()
CHROMEDRIVER_PATH = os.getenv('$HOME') or basedir+'/chromedriver.exe'
FLASK_CONFIG = os.getenv('FLASK_CONFIG')

if FLASK_CONFIG and FLASK_CONFIG == "production":
    options.binary_location = os.getenv('$GOOGLE_CHROME_SHIM')
    options.add_argument('--disable-gpu')
    options.add_argument('--no-sandbox')

driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, options=options)

This code works fine locally, but on heroku:

 driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, options=options)

2018-02-10T16:37:32.121783+00:00 app[web.1]: selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home

How do I set the path to the chromedriver in the buildpack?

like image 985
user1592380 Avatar asked Feb 10 '18 17:02

user1592380


People also ask

How do I change Chromedriver path?

Go to the terminal and type the command: sudo nano /etc/paths. Enter the password. At the bottom of the file, add the path of your ChromeDriver. Type Y to save.

Can I use Selenium in Heroku?

To use Selenium with Chrome on Heroku, you'll also need Chrome. We suggest one of these buildpacks: heroku-buildpack-google-chrome to run Chrome with the --headless flag. heroku-buildpack-xvfb-google-chrome to run Chrome against a virtual window server.


1 Answers

Not sure about the heroku-buildpack-xvfb-google-chrome buildpack; I'm using heroku/google-chrome

You can use this snippet to configure your definition

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

def load_chrome_driver(proxy):

      options = Options()

      options.binary_location = os.environ.get('GOOGLE_CHROME_BIN')

      options.add_argument('--headless')
      options.add_argument('--disable-gpu')
      options.add_argument('--no-sandbox')
      options.add_argument('--remote-debugging-port=9222')
      options.add_argument('--proxy-server='+proxy)

      return webdriver.Chrome(executable_path=str(os.environ.get('CHROMEDRIVER_PATH')), chrome_options=options)

I'm using proxies, but you can probably avoid that. Set the following path using heroku config:set command

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

like image 176
Ronnie Avatar answered Oct 23 '22 12:10

Ronnie