1. Computing

Text, Textarea, Password and Hidden Fields

By

These fields are handled by the Mechanize::Form::Field class. Simply use them like a string and you're done. This couldn't be any easier, and to illustrate that, we'll do the "Hello World" of the Mechanize world and open Google and search for something. The interesting part here is the q field, which is the text field you type your search queries into.


#!/usr/bin/env ruby
require 'rubygems'
require 'mechanize'

agent = Mechanize.new
page = agent.get('http://www.google.com/')
form = page.form_with(:action => '/search')

# Set the 'q' field like this
form.q = "Ruby Programming Language"

# You could read it like this, this is
# most useful if the server returns a form
# with some fields already filled in.
puts form.q

# Submit the search
search_results = form.submit
  1. About.com
  2. Computing
  3. Ruby
  4. Tutorials
  5. The Mechanize 2.0 Handbook
  6. Text, Textarea, Password and Hidden Fields

©2014 About.com. All rights reserved.