0

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>
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • It is expected that you will show an attempt to solve the problem. Stack Overflow is not a code-conversion or code-writing service. – the Tin Man Jan 21 '13 at 17:53

1 Answers1

1

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!

Prakash Murthy
  • 12,923
  • 3
  • 46
  • 74
  • Can you do a little bit change to the code using `mechanize`? Which can give me a good start. Please – Arup Rakshit Jan 21 '13 at 15:38
  • Edited the answer and added an example. Rather than use the non-existant `https://www.example.com/` site, I have used google/gmail in my example. Hope it helps! – Prakash Murthy Jan 21 '13 at 16:19
  • Thanks to you for giving a good tips, now I will use it to my actual one! – Arup Rakshit Jan 21 '13 at 18:21
  • I am getting an error when I ran the code. Could you please see the same? – Arup Rakshit Jan 21 '13 at 18:38
  • See http://stackoverflow.com/a/5973759/429758 for that issue. Best if you try out the other (smaller) examples from the mechanize documentation to familiarize yourself with the concepts first. – Prakash Murthy Jan 22 '13 at 01:24
  • Yes,I am done! Last night have built up my code till half of the total! Still it is good! Thanks for suggesting this gem – Arup Rakshit Jan 22 '13 at 04:27