Search code examples
mechanize-ruby

Add unnamed field to form. Mechanize Ruby


I am struggling to add fields to a form, because Mechanize doesn't detect the fields. I have 3 forms and I managed to select the correct form.

Entering p site.forms gives the following:

=> [#<Mechanize::Form
 {name "userloginform"}
 {method "POST"}
 {action "/user?destination="}
 {fields
  [text:0xc01f34 type: text name: name value: ]
  [field:0xc01cf0 type: password name: pass value: ]
  [hidden:0xc01ad4 type: hidden name: url value: en/Results+Statistics]
  [hidden:0xc01930 type: hidden name: form_id value: user_login]}
 {radiobuttons}
 {checkboxes}
 {file_uploads}
 {buttons [submit:0xc017c8 type: submit name: op value: Login]}>
, #<Mechanize::Form
 {name "searchFromBlock"}
 {method "GET"}
 {action ""}
 {fields
  [text:0xc04964 type: text name: queryString value: Search]
  [hidden:0xc044b4 type: hidden name: num value: 10]
  [hidden:0xc09f68 type: hidden name: pagenum value: 1]
  [hidden:0xc09ce8 type: hidden name: start value: 1]
  [hidden:0xc09ae0 type: hidden name: lang value: en]
  [hidden:0xc097e8 type: hidden name: content value: all]}
 {radiobuttons}
 {checkboxes}
 {file_uploads}
 {buttons [submit:0xc046f8 type: submit name: search value: ]}>
, #<Mechanize::Form
 {name nil}
 {method "GET"}
 {action ""}
 **{fields}**
 {radiobuttons}
 {checkboxes}
 {file_uploads}
 {buttons}>}>
  ]

I select the last form (which is my target), by: form = site.form[2]

The problem is that this form doesn't contain any fields according to Mechanize, but in reality it does contain another 3 fields. I want to manipulate these, but before I can do that I need to add the fields to the form object.

An example of such a field is:

<select id="season" class="w_label_resul fonts12" style="display: none" size="7">
<option title="1949" value="1949"> … </option>
<option title="1950" value="1950"> …</option>
<option title="1951" value="1951"> … </option>
<option title="2012" value="2012"> … </option>
<option title="2013" value="2013"> … </option>
<option class="selected" selected="" title="2014" value="2014"> … </option>
</select>

As you see, it has no 'name', so I can't use the add_field! method for Mechanize::form, because that takes as first argument the field_name, which is not present. Meaning it isn't properly parsed by Nokogiri?

Is there any other way to add a field manually or am I overlooking something?


Solution

  • I found out that the webpage is generated by javascript. This is probably why the form doesn't work.

    Reading through the java code, I found out how the weblink gets build up by the options you select. Using Nokogiri to iterate over these options, building the link and downloading the html source code, I was able to navigate to the places I wanted.