Hi all while I try to create an instance of IE I am facing the following error
my system configurations
Windows 7,64 bit
NameError: uninitialized constant Watir::IE
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'watir'
=> true
irb(main):003:0> ie=Watir::IE.new
NameError: uninitialized constant Watir::IE
from (irb):3
from D:/Ruby_1.8.7/lib/ruby/site_ruby/1.8/rubygems/specification.rb:630
irb(main):004:0>
To launch a browser in watir, you shoul use :
b = Watir::Browser.new :ie
# or :ff or :chrome or :opera ...
Note that for Internet explorer, you should download IEDriverServer.exe and add it to your path.
Related
Writing a standalone piece of code I can use
session.visit('/forms')
but how could I use
visit('/forms')
Code:
require 'webdrivers'
require 'rspec'
require 'capybara'
require 'capybara/rspec'
Capybara.configure do |config|
config.run_server = false
config.default_driver = :chrome
config.app_host = 'https://google.com'
end
options = Selenium::WebDriver::Chrome::Options.new(args: ['window-size=1200,1200'])
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
session = Capybara::Session.new(:chrome)
session.visit('/forms') # <-- this works :)
visit('/forms') # <-- but this doesn't :(
I get
undefined method `visit' for main:Object (NoMethodError)
One option is to use include Capybara::DSL, i.e.
include Capybara::DSL
session = Capybara::Session.new(:chrome)
visit('/forms')
However this does give a warning
including Capybara::DSL in the global scope is not recommended!
not sure how to get around that yet.
I was following this tutorial for screen scraping with Ruby and Watir.
I tried to write a simple script to return text from Wikipedia:
require "selenium-webdriver"
browser = Selenium::WebDriver.for :chrome
browser.get "https://wikipedia.org"
require "nokogiri"
puts doc.xpath(".//*[#id='langsearch-input']/p").inner_text
But when I run the script, I get this error in my terminal:
$ ruby app/views/layouts/scraper.rb
app/views/layouts/scraper.rb:7:in `<main>': undefined local variable or method `doc' for main:Object (NameError)
I have nokogiri 1.6.7.2, watir-webdriver 0.9.1, and watir 4.0.2 installed.
What am I doing wrong?
You are missing a line to that converts the browser HTML into a Nokogiri document. In other words, you have not defined what doc is.
require "selenium-webdriver"
browser = Selenium::WebDriver.for :chrome
browser.get "https://wikipedia.org"
require "nokogiri"
doc = Nokogiri::HTML.parse(browser.page_source)
puts doc.xpath(".//*[#id='langsearch-input']/p").inner_text
#=> ""
Note that while this will address the exception, the inner_text will return an empty string - ie "". The element with id "langsearch-input" is an input field, which dos not have a child p element or a text node.
Also note that you are not actually using Watir at all. To use Watir, it would look like:
require 'watir-webdriver'
browser = Watir::Browser.new :chrome
browser.goto "https://wikipedia.org"
require 'nokogiri'
doc = Nokogiri::HTML.parse(browser.html)
puts doc.xpath(".//*[#id='langsearch-input']/p").inner_text
#=> ""
However, unless you are doing a lot of parsing of a single large HTML chunk, using Watir without Nokogiri might be easier:
require 'watir-webdriver'
browser = Watir::Browser.new :chrome
browser.goto "https://wikipedia.org"
puts browser.text_field(id: 'langsearch-input').value
Browsermob proxy:-
https://github.com/jarib/browsermob-proxy-rb
I can able to create and set proxy for firefox profile but not on chrome.
I don't know which options i have to use for chrome to set proxy.
Am using the following code:-
For firefox:-
require 'selenium/webdriver'
require 'browsermob/proxy'
server = BrowserMob::Proxy::Server.new("/path/to/downloads/browsermob-proxy/bin/browsermob-proxy") #=> #<BrowserMob::Proxy::Server:0x000001022c6ea8 ...>
server.start
proxy = server.create_proxy #=> #<BrowserMob::Proxy::Client:0x0000010224bdc0 ...>
profile = Selenium::WebDriver::Firefox::Profile.new #=> #<Selenium::WebDriver::Firefox::Profile:0x000001022bf748 ...>
profile.proxy = proxy.selenium_proxy
driver = Selenium::WebDriver.for :firefox, :profile => profile
proxy.new_har "google"
driver.get "http://google.com"
har = proxy.har #=> #<HAR::Archive:0x-27066c42d7e75fa6>
har.entries.first.request.url #=> "http://google.com"
har.save_to "/tmp/google.har"
proxy.close
driver.quit
For chrome:-
require 'selenium/webdriver'
require 'browsermob/proxy'
server = BrowserMob::Proxy::Server.new("/path/to/downloads/browsermob-proxy/bin/browsermob-proxy") #=> #<BrowserMob::Proxy::Server:0x000001022c6ea8 ...>
server.start
proxy = server.create_proxy #=> #<BrowserMob::Proxy::Client:0x0000010224bdc0 ...>
profile = Selenium::WebDriver::Chrome::Profile.new #=>
profile.proxy = proxy.selenium_proxy
driver = Selenium::WebDriver.for :chrome, :prefs => profile
proxy.new_har "google"
driver.get "http://google.com"
har = proxy.har #=> #<HAR::Archive:0x-27066c42d7e75fa6>
har.entries.first.request.url #=> "http://google.com"
har.save_to "/tmp/google.har"
proxy.close
driver.quit
In chrome, errors throws on the following line
profile.proxy = proxy.selenium_proxy
Error:- NoMethodError: undefined method `proxy=' for #
How to set proxy on chrome driver profile ?
Hey not sure if resolve this issue already, I faced same problem but finally got that work by using this code:
require 'selenium/webdriver'
require 'browsermob/proxy'
server = BrowserMob::Proxy::Server.new("/path/to/downloads/browsermob-proxy/bin/browsermob-proxy") #=> #<BrowserMob::Proxy::Server:0x000001022c6ea8 ...>
server.start
proxy = Selenium::WebDriver::Proxy.new(:http => #proxy.selenium_proxy.http)
caps = Selenium::WebDriver::Remote::Capabilities.chrome(:proxy => proxy)
driver = Selenium::WebDriver.for(:chrome, :desired_capabilities => caps)
Then you can save the har file using har= proxy.har
By using this basically you avoiding to chromedriver to point to wrong port and localhost. Also this approach works also on IEdriver just change the caps to
Selenium::WebDriver::Remote::Capabilities.internetexplorer(:proxy => proxy)
Hope this approach helps :)
I'm new to ruby, I seem to have successfully installed watir-webdriver and webdriver-user-agent gems, but when I was trying to follow the instructions here I've stumbled. How to proceed?
>> require 'watir-webdriver'
=> true
>> require 'webdriver-user-agent'
=> true
>> driver = UserAgent.driver(:browser => :chrome, :agent => :iphone, :orientation => :landscape)
NameError: uninitialized constant UserAgent
from (irb):3
from /usr/bin/irb:12:in `<main>'
Try using Webdriver::UserAgent
I ran this example from git: https://github.com/andreasronge/neo4j
require "rubygems"
require 'neo4j'
Neo4j::Transaction.run do
node = Neo4j::Node.new(:name => 'andreas')
node.outgoing(:friends) << Neo4j::Node.new(:name => 'peter')
node.outgoing(:friends).each {|node| puts "name #{node[:name]}"}
end
and it outputted the error:
NameError: uninitialized constant Neo4j
const_missing at org/jruby/RubyModule.java:2626
(root) at ./neo4j.rb:4
require at org/jruby/RubyKernel.java:1027
require at ./neo4j.rb:36
(root) at /Users/ZATLUKE/RubymineProjects/nokogiri/neo4j.rb:2
load at org/jruby/RubyKernel.java:1052
(root) at -e:1
Any ideas?