我正在测试使用来自 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
最佳答案
你有没有重新启动你的 webrick,puma ...服务器?
奇迹般有效。gem 'selenium-webdriver', '~> 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
路线.rb:
get 'google-title' => 'application#google_title'
http://127.0.0.1:3000/google-title
结果谷歌
关于ruby-on-rails-4 - Ruby on Rails 无法加载此类文件 -- selenium-webdriver 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36138340/