Sha256: f2b9e0f47078db03c8155d021ccec690a5fc8d6b71b064bf4c7bde0e39fb4d6b

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

= WWW::Mechanize examples

== Google
  require 'rubygems'
  require 'mechanize'
  require 'logger'
  
  agent = WWW::Mechanize.new { |a| a.log = Logger.new("mech.log") }
  agent.user_agent_alias = 'Mac Safari'
  page = agent.get("http://www.google.com/")
  search_form = page.forms.find { |f| f.name == "f" }
  search_form.fields.find { |f| f.name == "q" }.value = "Hello"
  search_results = agent.submit(search_form)
  puts search_results.body

== Rubyforge
  require 'mechanize'
  
  agent = WWW::Mechanize.new {|a| a.log = Logger.new(STDERR) }
  page = agent.get('http://rubyforge.org/')
  link = page.links.find {|l| l.node.text =~ /Log In/ }
  page = agent.click(link)
  form = page.forms[1]
  form.fields.find {|f| f.name == 'form_loginname'}.value = ARGV[0]
  form.fields.find {|f| f.name == 'form_pw'}.value = ARGV[1]
  page = agent.submit(form, form.buttons.first)
  
  puts page.body

== File Upload
This example uploads one image as two different images to flickr.

  require 'rubygems'
  require 'mechanize'
  
  agent = WWW::Mechanize.new
  page = agent.get('http://flickr.com/signin/flickr/')
  form = page.forms.first
  form.fields.find { |f| f.name == 'email' }.value = ARGV[0]
  form.fields.find { |f| f.name == 'password' }.value = ARGV[1]
  page = agent.submit(form)
  page = agent.click(page.links.find { |l| l.text == 'Upload' })
  form = page.forms.first
  img1 = form.file_uploads.find { |f| f.name == 'file1' }
  img2 = form.file_uploads.find { |f| f.name == 'file2' }
  
  img1.file_name = img2.file_name = ARGV[2]
  File.open(ARGV[2], "r") { |f|
    img1.file_data = img2.file_data = f.read 
  }
  
  img1.mime_type = img2.mime_type = 'image/jpeg'
  
  agent.submit(form)
  

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mechanize-0.4.1 EXAMPLES
mechanize-0.4.2 EXAMPLES