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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With