ruby-on-rails - Heroku:使用 Selenium 时无法找到 chromedriver

标签 ruby-on-rails heroku webdriver buildpack

我有一个 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 的帖子
但我不知道如何正确配置 buildpack。
我试过:
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 中工作吗?

谢谢

更新:

我需要“网络驱动程序”,现在我得到了 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"

并这样做:
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 .

最佳答案

这样做的方法是:

  • 添加构建包
    heroku buildpacks:add https://github.com/heroku/heroku-buildpack-google-chrome
    heroku buildpacks:add https://github.com/heroku/heroku-buildpack-chromedriver
    
  • 添加环境变量 GOOGLE_CHROME_BINGOOGLE_CHROME_SHIM在 Heroku 中,两者都具有值(value) /app/.apt/opt/google/chrome/chrome IE。
    heroku config:set GOOGLE_CHROME_BIN=/app/.apt/opt/google/chrome/chrome
    heroku config:set GOOGLE_CHROME_SHIM=/app/.apt/opt/google/chrome/chrome
    
  • 按以下方式使用 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)
    
  • 关于ruby-on-rails - Heroku:使用 Selenium 时无法找到 chromedriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50688828/

    相关文章:

    java - 尝试在页面上定位文本元素

    ruby-on-rails - Rails 服务器运行,但 Rails 控制台抛出错误 "not checked out yet"

    ruby-on-rails - 在 Ruby 中的两个字符之间提取子链接

    ruby-on-rails - 是否可以从本地主机直接连接到 Heroku 数据库?

    java - Selenium Webdriver 和 Java。元素在点 (x, y) 处不可单击。其他元素将收到点击

    selenium - 使用 Selenium Web Driver 测试动态加载的内容

    ruby-on-rails - Ruby on Rails - 类别为哈希或数据库表

    ruby-on-rails - 为什么在运行 webrick 服务器时出现未初始化常量设计名称错误?

    node.js - 在heroku上运行最新的不稳定nodejs(例如0.11.6)

    ruby - 为什么 Heroku worker 有 100% dyno 负载?