Wednesday, March 30, 2016

Ruby on Rails cannot load such file — selenium-webdriver Error

Leave a Comment

I am testing running selenium webdriver with phantomjs from Ruby on Rails. I am testing locally on my computer.

The following code runs fine within my test Ruby script when I run "ruby test.rb"

  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 

I have pretty much the same code in my application_controller.rb file:

  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 

But when I run my app I get a "cannot load such file -- selenium-webdriver" error.

enter image description here

I have added this to my Gemfile and ran bundle install but get the same error.

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

When I run gem list --local it shows selenium is installed.

Any help would be appreciated.

Update:

I ran almost exactly the same code as above but with watir-webdriver and got the same results. The Ruby script ran fine by it's self but gave a "cannot load such file" error for watir-webdriver when I tried to run it from Rails.

I ran another test with nokogiri and it worked fine. Both by it's self and in Rails.

Update2:

It appears to be something I am doing and not my test environment. I just ran the same code on an online IDE and got the same type of error:

enter image description here

Update3:

Here is the full content of the application_controller.rb file when I run the test with watir-webdriver and headless. I get the same results. Maybe I am setting something wrong here?

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 

enter image description here

1 Answers

Answers 1

did you restart your webrick, puma ...server?

works like a charm.

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

bundle install

on 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' => 'application#google_title'

http://127.0.0.1:3000/google-title 

result Google

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment