如何在heroku chromedriver buildpack中设置chromedriver的路径

use*_*629 5 python selenium heroku selenium-chromedriver

我正在尝试在 heroku 上设置 selenium。我一直在寻找在Heroku 上使用 Python selenium 运行 ChromeDriver 以寻求帮助。基于此,我安装了列出的 2 个 buildback。我正在使用 cedar-14,因为不支持 16 堆栈。

当我运行时:

$ 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
Run Code Online (Sandbox Code Playgroud)

无论如何,我正在尝试使用

https://github.com/heroku/heroku-buildpack-chromedriver/tree/master/bin
Run Code Online (Sandbox Code Playgroud)

我的代码包含:

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)
Run Code Online (Sandbox Code Playgroud)

此代码在本地运行良好,但在 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
Run Code Online (Sandbox Code Playgroud)

如何在 buildpack 中设置 chromedriver 的路径?

Ron*_*nie 4

不确定 heroku-buildpack-xvfb-google-chrome buildpack;我正在使用 heroku/google-chrome

您可以使用此代码片段来配置您的定义

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)
Run Code Online (Sandbox Code Playgroud)

我正在使用代理,但你也许可以避免这种情况。heroku config:set使用命令设置以下路径

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