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

Hi Monks, I am trying to submit a webform using WWW::Mechanize. With the following code, only username field is getting filled. I am not sure why password field is not getting populated with value and thereby unable to submit the form. Any thoughts on this please ...

#!/usr/bin/perl use strict; use WWW::Mechanize; my $username = 'admin'; my $password = 'secret'; my $url = 'https://admin.testdomain.com'; my $mech = WWW::Mechanize->new; $mech->get($url); $mech->set_fields( 'ctl00$cntPlaceHolder$UsernameTextBox' => $username, 'ctl00$cntPlaceHolder$PasswordTextBox' => $password ); $mech->submit_form(); my $page = $mech->content(); print $page;

HTML Code after clicking view source

<td width="99%"> <input name="ctl00$cntPlaceHolder$UsernameTextBox"type="text" valu +e="admin" maxlength="60" id="ctl00_cntPlaceHolder_UsernameTextBox" cl +ass="textField" /><span class="MandatoryText">*</span> </td> <td width="99%"> <input name="ctl00$cntPlaceHolder$PasswordTextBox" type="password" + id="ctl00_cntPlaceHolder_PasswordTextBox" class="textField" /><span +class="MandatoryText">*</span> </td>

Replies are listed 'Best First'.
Re: WWW::Mechanize - Unable to fill password field
by 7stud (Deacon) on Jan 03, 2015 at 23:15 UTC

    Your code works fine for me in conjunction with the following html and cgi script:

    index.html:
    <!DOCTYPE html> <html> <head><title>Python CGI</title></head> <body> <form action="cgi-bin/mycgi.pl" method="post"> <div> <label for="name">User Name:</label> <input id="name" name="ctl00$cntPlaceHolder$UsernameTextBox" typ +e="text"> </div> <div> <label for="password">Last Name:</label> <input id="password" name="ctl00$cntPlaceHolder$PasswordTextBox" + type="password"> </div> <div> <input type="submit" value="submit"> </div> </form> </body> </html>
    mycgi.pl:
    #!/usr/bin/env perl use CGI; use CGI::Carp qw{ fatalsToBrowser }; my $cgi = CGI->new; print $cgi->header, $cgi->start_html('Test Page'), $cgi->h4('Params:'), $cgi->div('Name: ' . $cgi->param('ctl00$cntPlaceHolder$UsernameT +extBox')), $cgi->div('Password: ' . $cgi->param('ctl00$cntPlaceHolder$Passw +ordTextBox')), $cgi->end_html;

    HTML Code after clicking view source

    But what does your program output from the following line:

    print $page;

      When I print the page, I get the HTML code with the username filled with 'admin'. But, I don't see any value populated for password field. This is not allowing to submit the page.

      In my original post, I have given the code with username field with value but not for password field.

      Is this something related to SSO login of the website which is restricting to input password field

        why don't you submit what you need to without using set_fields...?
Re: WWW::Mechanize - Unable to fill password field
by Anonymous Monk on Jan 03, 2015 at 20:02 UTC
    If you can, look at the server-logs of the server that is receiving the information. Otherwise, a tool like WireShark can show you exactly what is being sent and received. Mech also has logging capabilities.