Search code examples
perlwww-mechanize

WWW::Mechanize text field issue


I'm trying to submit a form by post method using WWW::Mechanize perl module.

use WWW::Mechanize;

my $mech = WWW::Mechanize->new();
...
$mech->get($url);
...
my $response = $mech->submit_form(
        form_name => $name,
        fields    => {
                        $field_name => $field_value
                         },
        button    => 'Button'  
    );

$field_name is generally speaking a text field (though the type is not specified explicitly in the form), which has a preset value.

$field_name => $field_value in $mech->submit_form on whatever reason does not replace the value, instead $field_value is added into the form after the original value:

{submitted_field_value} = {original_value},{provided_value}

How to replace {original_value} with {provided_value} in the form to be submitted ?


Solution

  • I managed to make it working at my will. Thanks Timbus and knb for your suggestions. Though my case may not be completely general (I know the preset value) but I'd share what I've found (by trails & errors).

    my $mech = WWW::Mechanize->new();
    $mech->get($url);  
    $mech->form_name( $name );
    
    my $fields = $mech->form_name($name);
    foreach my $k ( @{$fields->{inputs}}){
     if ($k->{value} eq $default_value){
       $k->{value}=$field_value;
       }
    }
    
    my $response = $mech->click('Button_name');