Heroku:使用Selenium时无法找到chromedriver

Let*_*ron 5 ruby-on-rails webdriver heroku buildpack

我有一个Ruby代码,它执行此操作:

browser = Watir::Browser.new(:chrome, switches: switches, headless: true)
browser.goto(....)
Run Code Online (Sandbox Code Playgroud)

当我在Heroku上运行代码时,我得到了

Selenium::WebDriver::Error::WebDriverError:  Unable to find chromedriver. Please download the server from http://chromedriver.storage.googleapis.com/index.html and place it somewhere on your PATH. More info at https://github
.com/SeleniumHQ/selenium/wiki/ChromeDriver.
Run Code Online (Sandbox Code Playgroud)

我见过像Heroku这样的帖子:使用Watir/Selenium时无法连接到chromedriver 127.0.0.1:9515, 但我不知道如何正确配置buildpacks.我尝试过:

heroku buildpacks:set https://github.com/heroku/heroku-buildpack-google-chrome
heroku buildpacks:set https://github.com/heroku/heroku-buildpack-chromedriver
Run Code Online (Sandbox Code Playgroud)

但是当我尝试将更改推送到Heroku时,我得到:

Error Plugin: chromedriver: files attribute must be specified in /Users/leticia/.local/share/heroku/node_modules/chromedriver/package.json
Run Code Online (Sandbox Code Playgroud)

有人可以一步一步地告诉我如何设置必要的构建包以使Watir gem在Heroku中工作吗?

谢谢

更新:

我需要'网络驱动程序',现在我得到了 Selenium::WebDriver::Error::WebDriverError: unable to connect to chromedriver 127.0.0.1:9515.

我已经尝试将env vars配置为:

ENV['GOOGLE_CHROME_BIN'] = "/app/.apt/opt/google/chrome/chrome"
ENV['GOOGLE_CHROME_SHIM'] = "/app/.apt/usr/bin/google-chrome-stable"
Run Code Online (Sandbox Code Playgroud)

并这样做:

options = Selenium::WebDriver::Chrome::Options.new
chrome_bin_path = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
options.binary = chrome_bin_path if chrome_bin_path 
options.add_argument('--headless')
driver = Selenium::WebDriver.for :chrome, options: options
Run Code Online (Sandbox Code Playgroud)

但我仍然在最后一行得到错误.

更新2:

我搬到了Dokku而不是Heroku,我得到了同样的错误.Selenium::WebDriver::Error::WebDriverError: unable to connect to chromedriver 127.0.0.1:9515.

小智 8

做到这一点的方法是:

  1. 添加buildpacks

    heroku buildpacks:add https://github.com/heroku/heroku-buildpack-google-chrome
    heroku buildpacks:add https://github.com/heroku/heroku-buildpack-chromedriver
    
    Run Code Online (Sandbox Code Playgroud)
  2. 添加env vars GOOGLE_CHROME_BINGOOGLE_CHROME_SHIM在Heroku中都有值,/app/.apt/opt/google/chrome/chrome

    heroku config:set GOOGLE_CHROME_BIN=/app/.apt/opt/google/chrome/chrome
    heroku config:set GOOGLE_CHROME_SHIM=/app/.apt/opt/google/chrome/chrome
    
    Run Code Online (Sandbox Code Playgroud)
  3. 以下列方式使用watir

    args = %w[--disable-infobars --headless window-size=1600,1200 --no-sandbox --disable-gpu]
    options = {
           binary: ENV['GOOGLE_CHROME_BIN'],
           prefs: { password_manager_enable: false, credentials_enable_service: false },
           args:  args
         }    
    @browser = Watir::Browser.new(:chrome, options: options)
    
    Run Code Online (Sandbox Code Playgroud)