0

I am trying to write a web scraper using Watir that can be run on a schedule.

My module is called PriceScraperModule but it is not loading. I get this error:

NameError (uninitialized constant PriceScraperModule::Watir)

My module looks like:

module PriceScraperModule
  
  def self.scrape
    browser = Watir::Browser.new
  end
end

My Gemfile includes:

gem 'watir'
gem 'webdrivers'

When I try requiring it, it doesn't work either:

module PriceScraperModule
  require 'watir'
  
  def self.scrape
    browser = Watir::Browser.new
  end
end

I get this error:

LoadError (cannot load such file -- watir)

What should I do?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Noel Delgado
  • 173
  • 1
  • 2
  • 7
  • 2
    That looks very much like the gem is in your Gemfile but has not actually been installed. When you run ``bundle info watir``, what is the response? If it says 'could not find...', run ``bundle install`` and have another go. – rmlockerd Jul 27 '20 at 00:42
  • try to comment this line ` require 'watir'` as it is gem so no need for this line. – Kamal Panhwar Jul 27 '20 at 06:56
  • @KamalPanhwar and you're basing this assumption on what? If this code is not invoked through Rails you need to manually require the gem. The fact that the require is raising an error and not just returning false tells us that the gem is not installed on the system. – max Jul 27 '20 at 08:45
  • I have checked codes and there is no error, so I thought maybe in rails he is asking to require giving error! – Kamal Panhwar Jul 27 '20 at 09:19
  • @KamalPanhwar that's what I did on the first try. It doesn't have the require line. – Noel Delgado Aug 02 '20 at 15:17
  • @rmlockerd the response after running ´´´´ $ bundle info watir * watir (6.16.5) Summary: Watir powered by Selenium Homepage: http://github.com/watir/watir Path: /Users/noeldelgadom/.rvm/gems/ruby-2.5.1/gems/watir-6.16.5´´´´ – Noel Delgado Aug 02 '20 at 15:30

3 Answers3

0

I have written the following code and it's working properly.

require 'watir'
module PriceScraperModule
  def self.scrape
    b = Watir::Browser.new
    b.goto 'www.google.com'
  end
end

PriceScraperModule.scrape
Rajagopalan
  • 5,465
  • 2
  • 11
  • 29
0

Fixed it. In your terminal, run

$ spring stop
Noel Delgado
  • 173
  • 1
  • 2
  • 7
0

I was just checking as I did comment with perspective of Rails but you are not using rails so in case if you want to use it as a individual ruby project with bundler following will work.

Gemfile

# frozen_string_literal: true

source 'https://rubygems.org'

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gem 'watir'
gem 'webdrivers'

your module price_scraper_module.rb file

module PriceScraperModule
  require 'watir'

  def self.scrape
    browser = Watir::Browser.new
    browser.goto 'www.google.com'
  end
end

and now any file where you want to use I am using sample.rb

#!/usr/bin/ruby
$LOAD_PATH << '.'
require 'price_scraper_module'
include PriceScraperModule

PriceScraperModule.scrape

Now just run following commands to get all Gems

bundle

and to run the sample

ruby sample

Although your one file is perfectly working, in case you want to make it part of an individual project with Gemfile.

Kamal Panhwar
  • 2,345
  • 3
  • 21
  • 37