【问题标题】:Heroku: Unable to find chromedriver when using SeleniumHeroku:使用 Selenium 时无法找到 chromedriver
【发布时间】:2018-06-04 21:10:30
【问题描述】:

我有一个 Ruby 代码可以做到这一点:

browser = Watir::Browser.new(:chrome, switches: switches, headless: true)
browser.goto(....)

当我在 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.

我看过像Heroku: unable to connect to chromedriver 127.0.0.1:9515 when using Watir/Selenium 这样的帖子 但我不知道如何正确配置构建包。 我试过了:

heroku buildpacks:set https://github.com/heroku/heroku-buildpack-google-chrome
heroku buildpacks:set https://github.com/heroku/heroku-buildpack-chromedriver

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

Error Plugin: chromedriver: files attribute must be specified in /Users/leticia/.local/share/heroku/node_modules/chromedriver/package.json

谁能一步一步告诉我如何设置必要的构建包以使 Watir gem 在 Heroku 中工作?

谢谢

更新:

我需要“webdrivers”,现在我得到了Selenium::WebDriver::Error::WebDriverError: unable to connect to chromedriver 127.0.0.1:9515

我尝试将环境变量配置为:

ENV['GOOGLE_CHROME_BIN'] = "/app/.apt/opt/google/chrome/chrome"
ENV['GOOGLE_CHROME_SHIM'] = "/app/.apt/usr/bin/google-chrome-stable"

然后这样做:

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

但我仍然收到最后一行的错误。

更新 2:

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

【问题讨论】:

    标签: ruby-on-rails heroku webdriver buildpack


    【解决方案1】:

    方法是:

    1. 添加构建包

      heroku buildpacks:add https://github.com/heroku/heroku-buildpack-google-chrome
      heroku buildpacks:add https://github.com/heroku/heroku-buildpack-chromedriver
      
    2. 在 Heroku 中添加环境变量 GOOGLE_CHROME_BINGOOGLE_CHROME_SHIM,两者的值都为 /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
      
    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)
      

    【讨论】:

      【解决方案2】:

      我不能保证这足以满足您的所有需求,但如果您将 webdrivers 添加到您的 gemfile 并需要它,它会自动为您下载 chromedriver。

      【讨论】:

      • 我在 gemfile gem 'selenium-webdriver', '~> 3.12' 中添加,捆绑并添加到文件 require 'selenium-webdriver' 中,但我遇到了同样的错误。
      • 对不起。我重新阅读了您的答案并注意到您的意思是webdrivers 而不是selenium-webdriver,所以我在gemfile gem 'webdrivers', '~> 3.2' 和文件require 'webdrivers' 中添加了但我收到了错误:Selenium::WebDriver::Error::WebDriverError: unable to connect to chromedriver 127.0.0.1:9515
      • 好的,所以可能是权限问题,您需要构建包。看起来我可以解决 chromedriver 的获取,但不能解决它的运行。 :)
      • 支持 chromedriver 的事情。谢谢!但是你知道为什么我在使用 builpacks 推送时会收到错误插件吗?
      • 我从未尝试在 Heroku 上运行它,所以我不确定需要什么。
      猜你喜欢
      • 2018-04-29
      • 1970-01-01
      • 2019-11-30
      • 1970-01-01
      • 2022-01-10
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多