Pregunta

Im working on submitting the search form for yellowpages.com, but when I do pretty print page I notice in the name field it is blank. How would I tell mechanize to locate that form if the name field is blank?

require 'mechanize'

agent = Mechanize.new
page = agent.get('http://www.yellowpages.com')
pp page

Output:

#<Mechanize::Form
   {name nil}
   {method "GET"}
   {action "/search"}
   {fields
    [text:0x3fd6990db510 type: text name: search_terms value: ]
    [text:0x3fd6990db36c type: text name: geo_location_terms value: Dallas, TX]
    [hidden:0x3fd6990db13c type: hidden name: tracks value: true]}
   {radiobuttons}
   {checkboxes}
   {file_uploads}
   {buttons [button:0x3fd6990df87c type: submit name:  value: Search]}>}>
¿Fue útil?

Solución

The form has an action attribute; specify that information:

page.form_with(:action => '/search') do |form|
  ...
end

Otros consejos

You can go by the number it appears:

page.forms[0]

In fact, since it's the first one (and like lots of other "first" things) you can just do:

page.form
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top