在Heroku/Rails上使用Chromedriver:无法加载应用程序:Selenium::WebDriver::Error::WebDriverError:不是文件:“/usr/local/bin/chromedriver”

3
当我尝试在Heroku上运行Selenium时,出现了上述错误。我已经添加了heroku-buildpack-google-chrome和heroku-buildpack-chromedriver构建包,并添加了配置变量。
GOOGLE_CHROME_SHIM=/app/.apt/opt/google/chrome/chrome
GOOGLE_CHROME_BIN=/app/.apt/opt/google/chrome/chrome

在capybara设置中添加以下代码:

chrome_bin = ENV.fetch('GOOGLE_CHROME_SHIM', nil)

chrome_opts = chrome_bin ? { "chromeOptions" => { "binary" => chrome_bin } } : {}


Capybara.register_driver :chrome do |app|   Capybara::Selenium::Driver.new(
     app,
     browser: :chrome,
     desired_capabilities: Selenium::WebDriver::Remote::Capabilities.chrome(chrome_opts),   ) end

Capybara.javascript_driver = :chrome

如Chrome链接中所述:https://github.com/heroku/heroku-buildpack-google-chrome

我需要设置另一个变量来确定webdriver的位置吗?如果需要,应该如何设置?如何分配它?

谢谢!


我遇到了同样的问题。 - ecki
同样的问题,你解决了吗? - projectmind
1个回答

1

通过使用以下方法,我成功在Heroku Rails应用程序上实现了此操作:

1)添加了buildpacks heroku-buildpack-google-chrome和heroku-buildpack-chromedriver

2)在Rails中设置了驱动程序

options = Selenium::WebDriver::Chrome::Options.new
if Rails.env.production?
  Selenium::WebDriver::Chrome.path = ENV["GOOGLE_CHROME_SHIM"]
end
options.add_argument("--headless")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
@driver = Selenium::WebDriver.for :chrome, options: options

在Heroku上不需要设置任何ENV变量,因为webdriver构建包会自动设置GOOGLE_CHROME_SHIM。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接