goldfish_ has asked for the wisdom of the Perl Monks concerning the following question:

Hey there, I'm having some issues in filling out a form on a webpage with WWW::Mechanize that contains disabled fields.
bash-3.2$ mech-dump --forms "http://www.carzone.ie/usedcars/" GET http://www.carzone.ie/usedcars/index.cfm [form1] fuseaction=search (hidden readonly) MakeID= (option disabled) [*/Any Make] xMakeID= (hidden readonly)
My problem is the disabled fields, they seem to be generated by javascript unlike the others. I've searched around and I found someone who had a similar problem, they suggested using HTML::Form::ForceValue. So here is my code ...
use strict; use warnings; use WWW::Mechanize; use HTML::Form::ForceValue; my $m = WWW::Mechanize->new( autocheck => 1 ); $m->agent_alias( 'Windows IE 6' ); $m->get("http://www.carzone.ie/"); $m->follow_link( url_regex => qr/Used/i ); $m->set_fields( #MakeID => '32', #ModelID => '281', #CountyID => '10', xMakeID => '32', ); $m->form_name("form1")->find_input("MakeID")->force_value("32"); $m->form_name("form1")->dump;
And the output is the following:
GET http://www.carzone.ie/usedcars/index.cfm [form1] fuseaction=search (hidden readonly) MakeID=32 (option disabled) [:/Any Make|*32] xMakeID=32 (hidden readonly)
So it looks like it is setting the desired value, but the output is using 'Any Make', which suggests the forced value is being ignored. I'm pretty knew to this as a whole, so i'm not sure where to go from here. Any tips/advice would be greatly appreciated, thanks in advance.

Replies are listed 'Best First'.
Re: WWW::Mechanize and disabled form field
by Corion (Patriarch) on Nov 19, 2007 at 09:13 UTC

    Have you looked at what gets sent over the wire? I find the HTTP Live Headers plugin for FireFox quite useful but Wireshark is also good for monitoring what actually gets sent.

    It seems to me that xMakeID gets set to "32" and MakeID to "32" as well, so all should be good, but in the end what actually gets sent is what matters :) Maybe there are some additional parameters that get set by JavaScript that you haven't found out about yet.

Re: WWW::Mechanize and disabled form field
by saberworks (Curate) on Nov 19, 2007 at 09:32 UTC
    The problem is that disabled form fields do not get submitted when the form is submitted, they are completely ignored by browsers, so HTML::Form is also not submitting them. You already have a method call to find a field and force a value:

    $m->form_name("form1")->find_input("MakeID")->force_value("32");

    I looked at the HTML::Form documentation and there is a method called disabled() that you can use to toggle the "disabled" flag on a form field. So I added this to your code:

    $m->form_name('form1')->find_input('MakeID')->disabled(0);

    And then submitted it using mechanize, and I got back a list of volkswagons.

    The tricky part is that although the field is disabled by default, somewhere along the way, their javascript enables it. To use mechanize on javascript-enabled sites, you really have to look at what's being submitted to the server when you press submit on your browser, then you have to use mechanize to emulate that.
      Ah I see, thanks for the tips guys, i'll get to grips with wireshark and WWW::Mechanize::Shell for testing these things. Thanks alot.