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

Hi,

Im using WWW::Mechanize for screen scraping. The script will ocate the login form on the pages im visiting, and log on with credenticals. This works well, but after a time it crashes with the error:

Can't call method "value" on an undefined value at /usr/local/share/perl/5.8.8/WWW/Mechanize.pm line 1376.

I have a strong suspicion that this is based on the fact that mechanize did not find the textbox "username" on the site in question, for some reason. Maybe the site was down for a couple of seconds.

How should I do error handling on this? To ensure that the errors are logged, but the script does not stop ?

Here is some code to explaint what im doing

my $mech = WWW::Mechanize->new( agent =>$browser_agent); $mech->cookie_jar(HTTP::Cookies->new()); $mech->get($url_login); $mech->form_number($form_nummer); $mech->field('username' => $username); $mech->field('password' => $password); $mech->click(); <--- I think this is where the error gets trown, if t +he "username" textbox is not present

Replies are listed 'Best First'.
Re: Can't call method "value" on an undefined value at X??
by JavaFan (Canon) on Aug 16, 2010 at 10:25 UTC
    The mantra "always check your return values" isn't just for "open". You're calling ->get and then steamroll ahead without checking if the resource could actually be fetched. And then you do the same with ->form_number.
    I think this is where the error gets trown,
    You think? That would be trivial to check, wouldn't it? If that's what's triggering the error, you wouldn't see it if you'd exited the program just before.
Re: Can't call method "value" on an undefined value at X??
by moritz (Cardinal) on Aug 16, 2010 at 09:37 UTC
    At first sight, this looks like a bug in WWW::Mechanize. In such cases you should check the documentation if this is mentioned somewhere, and then look at the bug tracker if it's reported.

    If not, upgrade to the newest version of WWW::Mechanize, and check if the bug is still present. If yes, report it.

    How should I do error handling on this?

    You can inspect the returned HTML for the 'username' field, and if not present, try again. Yes, it's a workaround, not a fix.

    Perl 6 - links to (nearly) everything that is Perl 6.
      Looks like what happens when there is no form and you try to call field ...
Re: Can't call method "value" on an undefined value at X??
by Anonymous Monk on Aug 16, 2010 at 09:36 UTC
    eval BLOCK
    eval { $mech->get($url_login); $mech->form_number($form_nummer); $mech->field('username' => $username); $mech->field('password' => $password); $mech->click(); 1; } or warn "some call failed: $@";
Re: Can't call method "value" on an undefined value at X??
by ashish.kvarma (Monk) on Aug 16, 2010 at 11:54 UTC

    I have seen this error in two cases with Mechanize:

    1. Form does not exist.
    2. If form exists then the field called does not exist.

    A simple test would be the save the contents and check the form/fields in question do exists.

    Regards,
    Ashish
      Thank you all, a lot of good pointers here! :)

      Checking the HTML is very obvious, but nothing I came to think of .. so thanks. I agree that I should also check the request etc.

      Thanks :)
Re: Can't call method "value" on an undefined value at X??
by locked_user sundialsvc4 (Abbot) on Aug 16, 2010 at 15:17 UTC

    I agree with the (rather cryptic) advice to use eval, which is the Perl way of trapping an exception.   I would say, rather categorically, that you always want to write for the general case; for the way it works 99% of the time.   Don’t write if statements to check for what almost never happens.   Just be certain that an exception will, in fact, be thrown ... and then, catch it.

Re: Can't call method "value" on an undefined value at X??
by pkumar.pr (Initiate) on Aug 16, 2010 at 12:29 UTC

    I think problem lies with get and post method...Just make sure that other hidden values are also passed while posting the request.