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

Hi there Perlmonks! I have a small perl problem, I can't seem to solve. And I was hoping, someone could help me on it. I want to loginto a forum and then post a thread there. This should be done with WWW::Mechanize. The page source of the login site looks like this: <form action="http://www.forum.com/" method="get" onsubmit="return this.gotopage()" id="pagenav_form"> <input type="text" class="bginput" id="pagenav_itxt" style="font-size:11px" size="4" /> <input type="button" class="button" id="pagenav_ibtn" value="Go" /> </form> </td> </tr> </table> </div>    <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tr valign="top"> <td width="180" > <table class="tborder" cellpadding="6" cellspacing="1" border="0" width="100%" align="center"> <thead> <tr> <td class="tcat"><strong>Wellcome</strong></td> </tr> </thead> <tbody> <tr> <td class="alt1"> <table cellpadding="0" cellspacing="0" border="0" width="100%" align="center"> <tr> <td>  <form action="/login.php?do=login" method="post" onsubmit="md5hash(vb_login_password, vb_login_md5password, vb_login_md5password_utf, 0)"> <script type="text/javascript" src="clientscript/vbulletin_md5.js?v=382"></script> <table cellpadding="0" cellspacing="3" border="0" align="center"> <tr> <td align="center"><input type="text" class="namefield" style="font-size: 11px;width:105px;" name="vb_login_username" id="navbar_username" accesskey="u" tabindex="101" value="Username" onblur="if (this.value == '') this.value = 'Username';" onfocus="if (this.value == 'Username') this.value = '';" /></td> </tr> <tr> <td align="center"><input type="password" class="passfield" style="font-size: 11px;width:105px;" name="vb_login_password" id="navbar_password" tabindex="102" onblur="if (this.value == '') this.value = 'Password';" onfocus="if (this.value == 'Password') this.value = '';" /></td> </tr> <tr> <td class="smallfont" nowrap="nowrap" align="center" style="padding-left:-3px;"><label for="cb_cookieuser_navbar"><input type="checkbox" name="cookieuser" value="1" tabindex="103" id="cb_cookieuser_navbar" accesskey="c" />Stay signedin?</label></td></tr> <tr> <td align="center" style="padding:5px;"><input type="submit" class="button" value="Login" tabindex="104" title="Type in your username and password here!" accesskey="s" /></td> </tr> </table> My try to login:
use strict; use WWW::Mechanize; use HTTP::Cookies; use LWP; my $user = 'name'; my $pass = 'xx123xx'; my $url = "http://www.forum.com"; my $mech = WWW::Mechanize->new(); $mech->cookie_jar(HTTP::Cookies->new()); $mech->get($url); $mech->form_name("signin_form"); $mech->field('Username' => $user); $mech->field('Password' => $pass); $mech->submit();
I have tried many different things but i can not get it to work... And also this is wrong. Can anyone help? Thank you! :)

Replies are listed 'Best First'.
Re: Login in to forum with Mechanize
by runrig (Abbot) on Jul 02, 2013 at 19:52 UTC
    Looks like maybe there is JavaScript involved, and you can either use a JavaScript enabled library (e.g. WWW::Mechanize::Firefox, WWW::Selenium), or look more closely at what the real get and post data is with something like Firebug, and adjust your code accordingly.
Re: Login in to forum with Mechanize
by Loops (Curate) on Jul 02, 2013 at 19:48 UTC

    The problem is that the form uses a lot of javascript (for example to turn the password into an md5hash).

    You may be able to use WWW::Mechanize::Plugin::JavaScript, but you could also dig through to see what the javascript is doing and do each step yourself within perl code.

        how does a login with WWW::Scripter::Plugin::JavaScript work? i can not find any information to that!
      does that mean, if i would creat a md5 hash from my password myself it would work?

        does that mean, if i would creat a md5 hash from my password myself it would work?

        It might, but that might not be the only thing preventing login -- give it a try

        Hi KarlaCluft,
        I would try the $mechanize->submit_form(); function.
        Some thing like this maybe:
        $mech->get($url); $mech->submit_form( form_name => 'signin_form', fields => { 'Username' => $user, 'Password' => $pass} );

        If that doesn't work for you, do what runrig said. Also, I recently did a brief tutorial on WWW::Mechanize in our tutorials section if you want to take a look at that.
        UPDATE: With your current code, you could also try this instead of just $mech->click();
        $mech->click_button( value=> "Login");
        If that button is the one needed to submit the user and login and if submit_form doesn't work..