Can the description mentioned code using Selenium-driver be implemented in mechanize? - ruby

Could anyone help me by giving small tips mentioning how the below can be written in Mechanize? I am totally new to the Gem Mechanize.
require "rubygems"
require "selenium-webdriver"
driver = Selenium::WebDriver.for :firefox
driver.get "https://www.example.com/"
element = driver.find_element :name => "username"
element.send_keys "#####"
element = driver.find_element :name => "password"
element.send_keys "******"
element.submit
element = driver.find_element(:name, "btnHome")
element.click
element=driver.find_element(:link, "Empdetals")
#print element.attribute(:href)
element.click
element = driver.find_element :name => "search.empdirectory"
element.send_keys "#######"
element = driver.find_element :name => "btnSearch"
element.click
driver.current_url
ERROR When I tried the 'mechanzie` version provided by #Prakash
D:\Ruby script>ruby gmail.rb
C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net/http/persist
ent/ssl_reuse.rb:70:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 rea
d server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net
/http/persistent/ssl_reuse.rb:70:in `block in connect'
from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:54:in `timeout'
from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net
/http/persistent/ssl_reuse.rb:70:in `connect'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:750:in `start'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net
/http/persistent.rb:628:in `start'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net
/http/persistent.rb:570:in `connection_for'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.8/lib/net
/http/persistent.rb:926:in `request'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/mechanize-2.5.1/lib/mechanize/h
ttp/agent.rb:258:in `fetch'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/mechanize-2.5.1/lib/mechanize.r
b:407:in `get'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/mechanize-2.5.1/lib/mechanize.r
b:306:in `click'
from gmail.rb:6:in `block in <main>'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/mechanize-2.5.1/lib/mechanize.r
b:409:in `get'
from gmail.rb:4:in `<main>'
D:\Ruby script>

Yes, mechanize gem can be used to automate any interaction with a website, including logging into a site by submitting the userid/password and clicking on submit button/link, etc.
Unlike selenium-webdriver which makes direct calls to a browser, mechanize itself acts as a browser.
Do checkout the EXAMPLES page on mechanize documentation to learn to how mechanize can be used. The second example - with RubyForge - shows how to login to a site and work with the resultant page.
For a quick overview of how to work with mechanize check out RailsCasts episode on mechanize
Here is an example code for starting at http://www.google.com, clicking on 'Gmail' text, signing into Gmail, and listing the links within the page:
require 'mechanize'
a = Mechanize.new
a.get('http://www.google.com') do |page|
# Click the Gmail link
gmail_login_page = a.click(page.link_with(:text => "Gmail"))
# Submit the login form
gmail_page = gmail_login_page.form_with( :action => 'https://accounts.google.com/ServiceLoginAuth' ) do |f|
f.Email = "<username>#gmail.com"
f.Passwd = "**********"
end.click_button
# List all the links in the personal gmail page
gmail_page.links.each do |link|
text = link.text.strip
next unless text.length > 0
puts text
end
end
Hope it helps in getting started with Mechanize and explore it further!

Related

unable to locate element in Ruby simple script fail

When I run the script:
require 'rubygems'
require 'watir-webdriver'
#browser = Watir::Browser.new :firefox
def adopt_puppy_number(num)
#browser.button(:value => 'View details', :index => num - 1).click
#browser.button(:value => 'Adopt Me!').click
end
#browser.goto 'http://puppies.herokuapp.com'
adopt_puppy_number 1
#browser.button(:value => 'Adopt Another Puppy').click
adopt_puppy_number 2
I get the exception:
/var/lib/gems/1.9.1/gems/watir-webdriver-0.9.1/lib/watir-webdriver/elements/element.rb:536:in `assert_element_found': unable to locate element, using {:value=>"View details", :index=>0, :tag_name=>"button"} (Watir::Exception::UnknownObjectException)
from /var/lib/gems/1.9.1/gems/watir-webdriver-0.9.1/lib/watir-webdriver/elements/element.rb:508:in `assert_exists'
from /var/lib/gems/1.9.1/gems/watir-webdriver-0.9.1/lib/watir-webdriver/elements/element.rb:114:in `click'
from /home/giz/Downloads/RubymineProjects/learn_watir/thirdscript_dry.rb:6:in `adopt_puppy_number'
from /home/giz/Downloads/RubymineProjects/learn_watir/thirdscript_dry.rb:11:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
Why? Maybe I should add a wait command, but where exactly and which one?
The HTML for the View Details button is:
<input class="rounded_button" value="View Details" type="submit">
Notice that the value attribute is "View Details" not "View details". Watir's locators are case-sensitive, which is the element is not found. The adopt_puppy_number method should be:
def adopt_puppy_number(num)
#browser.button(:value => 'View Details', :index => num - 1).click
#browser.button(:value => 'Adopt Me!').click
end

Need to implement Watirgrid,

I want to implement watirgrid, but I'm not able to do that, every time I'm getting errors related with controller and provider starting process, Also all the example over internet, none of them are working.
Could any one please help me to implement this, a full running example with steps will be a great help.
I'm trying this code:
require 'rubygems'
require 'watirgrid'
require 'watir'
require 'watir-webdriver'
# setup a controller on port 12351 for your new grid
controller = Controller.new(:ring_server_port => 12351, :loglevel => Logger::ERROR)
controller.start
# add a provider to your grid
# :browser_type => 'webdriver' if using webdriver or
# :browser_type => 'ie' if using watir...
provider = Provider.new(:controller_uri => 'druby://127.0.0.1:11235',
:ring_server_port => 12351,
:loglevel => Logger::ERROR,
:browser_type => 'webdriver')
provider.start
# connect to the grid and take all providers from it (this time only one)
grid = Watir::Grid.new(:ring_server_port => 12351, :ring_server_host => '127.0.0.1')
grid.start(:take_all => true)
# for each provider on the grid, launch a new thread to start multiple browsers
threads = []
grid.browsers.each do |browser|
threads << Thread.new do
p browser[:hostname]
p browser[:architecture]
p browser[:browser_type]
# in this case we are starting a new IE browser
b = browser[:object].new_browser(:ie)
b.goto("http://www.google.com")
b.text_field(:name, 'q').set("watirgrid")
b.button(:name, "btnI").click
end
end
threads.each {|thread| thread.join}
And Errors I'm getting is
DRb::DRbConnError: druby://127.0.0.1:11235 - #<Errno::ECONNREFUSED: No connection could
be made because the target machine actively refused it. - connect(2)>
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:736:in `rescue in block in open'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:730:in `block in open'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:729:in `each'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:729:in `open'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:1191:in `initialize'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:1171:in `new'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:1171:in `open'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:1087:in `block in method_missing'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:1105:in `with_friend'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:1086:in `method_missing'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watirgrid-1.1.5/lib/provider.rb:141:in `start'
from (irb):44
from C:/Ruby192/bin/irb:12:in `<main>'
irb(main):045:0>
I got the same problem, I got the provider started successfully by changing the
controller_uri
to
controller_uri => 'druby://machineIPAddress:11235'

need ignore basic http auth

I have trouble with getting page source.
require 'mechanize'
agent = Mechanize.new
page = agent.get("https://#{ip}/")
end have error:
/home/lord/.gem/ruby/1.9.1/gems/mechanize-2.4/lib/mechanize/http/agent.rb:682:in `response_authenticate': 401 => Net::HTTPUnauthorized for https://82.144.208.6/cgi-bin/welcome.cgi -- no credentials found, provide some with #add_auth -- available realms: r722 (Mechanize::UnauthorizedError)
from /home/lord/.gem/ruby/1.9.1/gems/mechanize-2.4/lib/mechanize/http/agent.rb:288:in `fetch'
from /home/lord/.gem/ruby/1.9.1/gems/mechanize-2.4/lib/mechanize.rb:407:in `get'
from /home/lord/ruby/ruby_backup/backup-done.ru:35:in `block (2 levels) in <main>'
how can I ignore http auth, and get source? thx
The exception contains a page accessor. The documentation describes it: http://mechanize.rubyforge.org/Mechanize/ResponseCodeError.html
Try:
begin
page = agent.get ...
rescue Mechanize::ResponseCodeError => e
page = e.page
end

Using mechanize (or even Net::HTTP) with ruby 1.9 returns 'OpenSSL::SSL::SSLError' - how to force SSLv3?

For the past 2 days, I think I've been through every single available (google'able) post about this SSL-Error in connection with Net::HTTP:
OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A
What I tried doing in the first place, was connecting to a EtherPad server (https://test.titanpad.com), signing in, and downloading a zip-archive via using rubygems & mechanize; however, unfortunately I don't even get up to that point due to said SSL-Error. After trying to debug the issue from within a mechanize object (f.e. by setting cert, ca_file, cert_store, verify_mode, etc. manually in the script), I went one level closer to the actual issue, trying to connect to https://test.titanpad.com by simply using Net::HTTP:
(in this example, I first connected to https://encrypted.google.com to make sure, SSL should work out; the attempt to connect to the EtherPad server starts from line 6)
irb(main):001:0> require 'net/https'
=> true
irb(main):002:0> google = Net::HTTP.new('encrypted.google.com', 443)
=> #<Net::HTTP encrypted.google.com:443 open=false>
irb(main):003:0> google.use_ssl = true
=> true
irb(main):004:0> google.ca_file = '/opt/local/share/curl/curl-ca-bundle.crt' if File.exists?('/opt/local/share/curl/curl-ca-bundle.crt')
=> "/opt/local/share/curl/curl-ca-bundle.crt"
irb(main):005:0> google.request_get('/')
=> #<Net::HTTPOK 200 OK readbody=true>
irb(main):006:0> etherpad = Net::HTTP.new('test.titanpad.com', 443)
=> #<Net::HTTP test.titanpad.com:443 open=false>
irb(main):007:0> etherpad.use_ssl = true
=> true
irb(main):008:0> etherpad.ca_file = '/opt/local/share/curl/curl-ca-bundle.crt' if File.exists?('/opt/local/share/curl/curl-ca-bundle.crt')
=> "/opt/local/share/curl/curl-ca-bundle.crt"
irb(main):009:0> etherpad.request_get('/')
OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:799:in `connect'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:799:in `block in connect'
from /opt/local/lib/ruby1.9/1.9.1/timeout.rb:54:in `timeout'
from /opt/local/lib/ruby1.9/1.9.1/timeout.rb:99:in `timeout'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:799:in `connect'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:755:in `do_start'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:744:in `start'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1284:in `request'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1195:in `request_get'
from (irb):9
from /opt/local/bin/irb:12:in `<main>'
Even when using verify_mode OpenSSL::SSL::VERIFY_NONE, OpenSSL bails out:
irb(main):010:0> etherpad.verify_mode = OpenSSL::SSL::VERIFY_NONE
=> 0
irb(main):011:0> etherpad.request_get('/')
OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:799:in `connect'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:799:in `block in connect'
from /opt/local/lib/ruby1.9/1.9.1/timeout.rb:54:in `timeout'
from /opt/local/lib/ruby1.9/1.9.1/timeout.rb:99:in `timeout'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:799:in `connect'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:755:in `do_start'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:744:in `start'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1284:in `request'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1195:in `request_get'
from (irb):11
from /opt/local/bin/irb:12:in `<main>'
After further playing around with openssl itself, it turns out the real trouble in this case is that the use of SSLv3 has to be forced for the handshake with the Jetty 6.1.20 server behind titanpad.com to work:
irb(main):001:0> require 'net/https'
=> true
irb(main):002:0> etherpad = Net::HTTP.new('test.titanpad.com', 443)
=> #<Net::HTTP test.titanpad.com:443 open=false>
irb(main):003:0> etherpad.use_ssl = true
=> true
irb(main):004:0> etherpad.ssl_version = "SSLv3"
=> "SSLv3"
irb(main):005:0> etherpad.ca_file = '/opt/local/share/curl/curl-ca-bundle.crt' if File.exists?('/opt/local/share/curl/curl-ca-bundle.crt')
=> "/opt/local/share/curl/curl-ca-bundle.crt"
irb(main):006:0> etherpad.request_get('/')
=> #<Net::HTTPFound 302 Found readbody=true>
Now while this obviously works when using Net::HTTP, there is no such option as to set the SSL version to use in Mechanize... and I'd therefore be really glad if someone could point out to me as to how I could enforce SSLv3 via said gem o.O
Thanks again!
System: Mac OSX 10.6.8
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10]
rubygems installed with mechanize: domain_name (0.5.2), mechanize (2.1.1), net-http-digest_auth (1.2), net-http-persistent (2.4.1), nokogiri (1.5.0), ntlm-http (0.1.1), unf (0.0.4), unf_ext (0.0.4), webrobots (0.0.13)
Has been fixed by porting the ssl_version functionality from Net::HTTP (via net-http-persistent) to Mechanize v. 2.1.2 (see https://github.com/tenderlove/mechanize/commit/4a228899855e0676ab69c2bf548170c8717465d8).

Watir/Selenium - browser.goto keep getting TimeOut error on Chrome and Firefox

Got a very annoying problem with Watir webdriver..
I have debugged a little, and found out I always get TimeOut::Error on a simple #browser.goto line, even I can visually see the page has loaded fully...
The scenario is like this:
Open a browser, goto a url, click a few links, and then suddenly at one point, the script stops continue browsing, waiting about 30+ seconds and throw errors.
Tried both Chrome and FF: Chrome is much worse, normally a 2nd or 3nd link clicking will trigger; for FF, sometimes takes 10+ page browsing...
Bet there is some environment or comparability issue:
jd#deskbox:~$ uname -am
Linux deskbox 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
jd#deskbox:~$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
jd#deskbox:~$ rails -v
Rails 3.2.1
jd#deskbox:~$ gem -v
1.8.15
jd#deskbox:~$ gem list|grep webdriver
selenium-webdriver (2.12.0)
watir-webdriver (0.5.3)
Can someone help on this? Source code here:
#!/usr/bin/env ruby
require 'watir-webdriver'
require 'pry'
class Search
attr_accessor :browser, :company_url, :company_name
def initialize()
#browser = Watir::Browser.start 'http://www.google.com', :chrome
end
def visit_company_home_via_google(company)
#company_name = company
#browser.goto "http://www.google.com/search?q=#{company}"
link = #browser.div(:id=>'ires').link
return nil unless link.exists?
#browser.goto link.href
#company_url ||= #browser.url
#company_url
end
def logoff()
#browser.close if #browser
end
end
s = Search.new
puts s.visit_company_home_via_google("github")
puts s.visit_company_home_via_google("Mashable")
puts s.visit_company_home_via_google("Barracuda Networks")
s.logoff
My result is like:
jd#deskbox:~/cuda$ ./search.rb
https://github.com/
/usr/local/lib/ruby/1.9.1/net/protocol.rb:140:in `rescue in rbuf_fill': Timeout::Error (Timeout::Error)
from /usr/local/lib/ruby/1.9.1/net/protocol.rb:134:in `rbuf_fill'
from /usr/local/lib/ruby/1.9.1/net/protocol.rb:116:in `readuntil'
from /usr/local/lib/ruby/1.9.1/net/protocol.rb:126:in `readline'
from /usr/local/lib/ruby/1.9.1/net/http.rb:2219:in `read_status_line'
from /usr/local/lib/ruby/1.9.1/net/http.rb:2208:in `read_new'
from /usr/local/lib/ruby/1.9.1/net/http.rb:1191:in `transport_request'
from /usr/local/lib/ruby/1.9.1/net/http.rb:1177:in `request'
from /usr/local/lib/ruby/1.9.1/net/http.rb:1170:in `block in request'
from /usr/local/lib/ruby/1.9.1/net/http.rb:627:in `start'
from /usr/local/lib/ruby/1.9.1/net/http.rb:1168:in `request'
from /usr/local/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.12.0/lib/selenium/webdriver/remote/http/default.rb:81:in `response_for'
from /usr/local/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.12.0/lib/selenium/webdriver/remote/http/default.rb:43:in `request'
from /usr/local/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.12.0/lib/selenium/webdriver/remote/http/common.rb:39:in `call'
from /usr/local/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.12.0/lib/selenium/webdriver/remote/bridge.rb:450:in `raw_execute'
from /usr/local/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.12.0/lib/selenium/webdriver/remote/bridge.rb:428:in `execute'
from /usr/local/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.12.0/lib/selenium/webdriver/remote/bridge.rb:99:in `get'
from /usr/local/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.12.0/lib/selenium/webdriver/common/navigation.rb:14:in `to'
from /usr/local/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.5.3/lib/watir-webdriver/browser.rb:61:in `goto'
from ./search.rb:17:in `visit_company_home_via_google'
from ./search.rb:33:in `<main>'
I think there's a bug in Chromedriver that doesn't return the URL correctly. I got your example to work using:
require 'watir-webdriver'
class Search
attr_accessor :browser, :company_name
def initialize
#browser = Watir::Browser.start 'http://www.google.com', :chrome
end
def get_company_url(company, url=nil)
#company_name = company
#company_url = url
#browser.goto "http://www.google.com/search?q=#{company}"
link = #browser.div(:id=>'ires').link
return nil unless link.exists?
#company_url ||= #browser.driver.current_url
end
def logoff()
#browser.close if #browser
end
end
s = Search.new
puts s.get_company_url 'Barracuda Networks'

Resources