Skip to content

Instantly share code, notes, and snippets.

@stungeye
Created May 11, 2011 15:37
Show Gist options
  • Save stungeye/966689 to your computer and use it in GitHub Desktop.
Save stungeye/966689 to your computer and use it in GitHub Desktop.
Mechanize Example
# Example of using Mechanize to login to the RBC online banking.
# RBC has since changed the structure of their HTML, so I doubt this script still works.
require 'rubygems'
require 'hpricot'
require 'mechanize'
WWW::Mechanize.html_parser = Hpricot
agent = WWW::Mechanize.new
page = agent.get('https://www1.royalbank.com/cgi-bin/rbaccess/rbunxcgi?F6=1&F7=IB&F21=IB&F22=IB&REQUEST=ClientSignin&LANGUAGE=ENGLISH')
form = page.forms[1]
form.K1 = '4519XXXXXXXXXXXX'
form.Q1 = 'password'
form.NOJAVASCRIPT = nil
page = agent.submit(form)
signout = nil
page.search('//a').each do |link|
if (link.inner_html == 'Sign Out')
signout = link
end
end
if (signout)
puts "Login Successful"
page.search('select#ACCOUNT option').each do |option|
puts option.inner_html.strip
end
agent.click signout
puts "Signed Out"
else
put "Could Not Login"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment