【问题标题】:Ruby on Rails cannot load such file -- selenium-webdriver ErrorRuby on Rails 无法加载此类文件——selenium-webdriver 错误
【发布时间】:2016-03-21 17:56:32
【问题描述】:

我正在使用 Ruby on Rails 中的 phantomjs 测试运行 selenium webdriver。我正在我的电脑上进行本地测试。

当我运行“ruby test.rb”时,以下代码在我的测试 Ruby 脚本中运行良好

  def google_title
    require 'selenium-webdriver'
    driver = Selenium::WebDriver.for :phantomjs
    driver.navigate.to "https://www.google.com"
    page_title = driver.title
    puts page_title
  end

  google_title

我的 application_controller.rb 文件中有几乎相同的代码:

  def google_title
    require 'selenium-webdriver'
    driver = Selenium::WebDriver.for :phantomjs
    driver.navigate.to "https://www.google.com"
    page_title = driver.title
    render text: page_title
  end

但是当我运行我的应用程序时,我收到“无法加载此类文件 -- selenium-webdriver”错误。

我已将此添加到我的 Gemfile 并运行 bundle install 但得到了同样的错误。

gem 'selenium-webdriver', '~> 2.45'

当我运行 gem list --local 时,它显示 selenium 已安装。

任何帮助将不胜感激。

更新:

我运行了与上面几乎完全相同的代码,但使用了 watir-webdriver 并得到了相同的结果。 Ruby 脚本本身运行良好,但当我尝试从 Rails 运行它时,给 watir-webdriver 一个“无法加载此类文件”错误。

我用 nokogiri 进行了另一个测试,它运行良好。通过它自己和在 Rails 中。

更新2:

这似乎是我正在做的事情,而不是我的测试环境。我刚刚在在线 IDE 上运行了相同的代码并得到了相同类型的错误:

更新3:

这是我使用 watir-webdriver 和 headless 运行测试时 application_controller.rb 文件的完整内容。我得到相同的结果。也许我在这里设置了错误?

class ApplicationController < ActionController::Base
  def google_title
    require 'watir-webdriver'
    require 'headless'
    headless = Headless.new
    headless.start
    b = Watir::Browser.start 'www.google.com'
    page_title = b.title
    b.close
    headless.destroy
    render text: page_title
  end
end

【问题讨论】:

  • 我无法重现此问题。我尝试了完全相同的步骤并安装了selenium-webdriver 版本2.53.0,它就像一个魅力。
  • 奇怪。我添加了 application_controller.rb 文件的全部内容,只是为了确保我没有遗漏一些简单的东西。
  • github.com/firangistan/webdriver-test 检查此应用程序是否适合您。
  • 您在使用任何版本管理器吗?你尝试过捆绑更新吗?

标签: ruby-on-rails-4 selenium-webdriver phantomjs


【解决方案1】:

你重启你的webrick,puma ...服务器了吗?

像魅力一样工作。

gem 'selenium-webdriver', '~&gt; 2.45'

bundle install

在 Mac 上:

brew install phantomjs

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception
  #no need for require
  def google_title
    driver = Selenium::WebDriver.for :phantomjs
    driver.navigate.to "https://www.google.com"
    page_title = driver.title
    render text: page_title
  end
end

routes.rb:

get 'google-title' =&gt; 'application#google_title'

http://127.0.0.1:3000/google-title

结果谷歌

【讨论】:

    猜你喜欢
    • 2022-10-23
    • 2012-12-03
    • 2014-04-23
    • 2014-04-24
    • 2018-10-19
    • 1970-01-01
    • 2012-03-09
    • 2015-05-09
    • 2018-05-22
    相关资源
    最近更新 更多