如何在heroku chromedriver构建包中设置chromedriver的路径

envsm3lx  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(146)

我尝试在heroku上设置selenium。我一直在寻找Running ChromeDriver with Python selenium on Heroku的帮助。基于此,我安装了列出的2个构建。我使用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

无论如何我都想用

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

我的程式码包含:

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)

这段代码在本地可以正常工作,但在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

如何在构建包中设置chromedriver的路径?

q5iwbnjs

q5iwbnjs1#

不确定heroku构建包-xvfb-google-chrome构建包;我用的是heroku/谷歌铬
您可以使用此代码段来配置定义

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)

我使用的是代理服务器,但您可能可以避免使用代理服务器。
CHROMEDRIVER_PATH=/app/.chromedriver/bin/chromedriverGOOGLE_CHROME_BIN=/app/.apt/usr/bin/google-chrome

ux6nzvsh

ux6nzvsh2#

这对我很有效:

options = Selenium::WebDriver::Chrome::Options.new
  Selenium::WebDriver::Chrome::Service.driver_path = '/app/.chromedriver/bin/chromedriver'
  chrome_bin_path = ENV.fetch('GOOGLE_CHROME_SHIM', nil)

  if chrome_bin_path
    options.binary = chrome_bin_path if chrome_bin_path
    options.add_argument '--no-sandbox'
    options.add_argument '--window-size=1200x600'
    options.add_argument '--headless'
    options.add_argument '--disable-gpu'
  end
  
  browser = Watir::Browser.new(:chrome, options:)

相关问题