How do I install chromedriver in a specific directory (/app/assets/chromedriver) on Heroku?

风流意气都作罢 提交于 2019-12-11 08:46:13

问题


Problem

I have been playing around with a Python script using Selenium, which works fine locally (as I have ChromeDriver installed in the correct directory, which is assets/chromedriver), but it does not work on Heroku. A summary of the error message I get is this:

'chromedriver' executable needs to be in PATH.
No such file or directory: '/app/assets/chromedriver': '/app/assets/chromedriver'
ensure chromedriver is installed at /app/assets/chromedriver

Steps Taken

When deploying to Heroku, I have installed the following Buildpacks on my app:

  1. https://github.com/heroku/heroku-buildpack-python
  2. https://github.com/heroku/heroku-buildpack-chromedriver
  3. https://github.com/heroku/heroku-buildpack-google-chrome

(Note: a lot of the answers on StackOverflow talk about the heroku-xvfb-google-chrome buildpack instead, but I don't want to use that because it relies on Cedar-14, which is being deprecated in April this year.)

I've attempted to set $GOOGLE_CHROME_BIN and $GOOGLE_CHROME_SHIM as config variables pointing to the app/assets/chromedriver directory, but this hasn't worked. Does anyone have any idea of how to get chromedriver installed in a specific directory (in this case, app/assets/chromedriver) on Heroku?

I've been struggling with this for days now, and would really appreciate any help I can get!


回答1:


Set the following path using heroku congfig:set command

heroku config:set CHROMEDRIVER_PATH=/app/.chromedriver/bin/chromedriver and heroku config:set GOOGLE_CHROME_BIN=/app/.apt/usr/bin/google-chrome

Verify the paths using heroku config command

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.



来源:https://stackoverflow.com/questions/54181339/how-do-i-install-chromedriver-in-a-specific-directory-app-assets-chromedriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!