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

The following code:
$mech->get("http://indiecharts.com/"); $mech->cookie_jar( HTTP::Cookies->new() ); if( $mech->content() =~ /<form Method="Post" action="login.asp"> / ){ $mech->form_number($fn); $mech->field( UserName => $usr ); $mech->field( Password => $pw ); $mech->submit(); if ($mech->success() ) { if ( $mech->content() =~ /Pick your IndieCharts Name/ ) { print "User $usr logged in successfully!\n" ;
Is used to login to a website.
Works successfully on first script I wrote, but on another script it says that "No such field 'UserName' at C:/Perl/site/lib/WWW/Mechanize.pm line 538"
What gives? Why would it work for one script but not another? I even try them back to back, and one works, then the other doesn't...

Replies are listed 'Best First'.
Re: Mechanize Form Issue?
by Cody Pendant (Prior) on May 02, 2007 at 07:14 UTC
    Your question isn't exactly clear -- you have two different scripts, which you're running from the same computer at the same time, and they contain the same login code, but one works and the other doesn't?

    Sounds unlikely.

    But obviously, the sensible thing to do is just print out $mech->content() before you try to log in and examine the HTML.

    Your test basically says "if there's a certain string anywhere on the page, assume I'm not logged in and try to log in". There may be something wrong with that logic -- another form on the page which looks like that, or the same form commented out? Whatever it turns out to be, the answer will almost certainly be in the HTML.



    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
Re: Mechanize Form Issue?
by GrandFather (Saint) on May 01, 2007 at 22:45 UTC

    That page takes a very long time to load in a browser. Could you be having timeout issues?


    DWIM is Perl's answer to Gödel
      I added some code to print after the page is loaded to check if the page loaded successfully, and it loads correctly. So i dont think thats my problem
Re: Mechanize Form Issue?
by polettix (Vicar) on May 02, 2007 at 15:33 UTC
    $mech->form_number($fn);
    Are you sure you're selecting the correct form? Is the value of $fn the same for the two scripts?

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.