2

I am just doing a testing using WWW::Mechanize module on Facebook, when I try to run the code below, it return me an error

Can't call method "header" on an undefined value at C:/Strawberry/perl/vendor/lib/WWW/Mechanize.pm line 2566.

#!/usr/bin/perl -w
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
# Connect to server
$mech->get( "https://www.facebook.com" );
$mech->success or die $mech->response->status_line;

# Log into server

$mech->field('email', 'xxx@xxx.com');
$mech->field('pass', 'xxxxxxx');
$mech->click_button(value => 'Log In');
olibiaz
  • 2,551
  • 4
  • 29
  • 31
Zachary
  • 75
  • 8
  • This works fine for me. Please check which statement in your code is causing the error. What is the output from `perl -MLWP -E 'say $LWP::VERSION'` and `perl -MWWW::Mechanize -E 'say $WWW::Mechanize::VERSION'`? Note that you should *always* `use strict` and `use warnings 'all'` at the top of *every* Perl program you write. There is no point in using `my` at all without `use strict` in place, and there is no need for `-w` on the shebang line – Borodin May 04 '16 at 04:20
  • $LWP::VERSION give me "Can't locate object method "say" via package "6.15" (perhaps you forgot to load "6.15"?" and Mechanize::VERSION is 1.75 – Zachary May 04 '16 at 04:30
  • You must have a very old version of Perl? – Borodin May 04 '16 at 04:31
  • I just install from http://strawberryperl.com/ this morning with the version of Strawberry Perl 5.22.1.3 (64bit) – Zachary May 04 '16 at 04:35
  • Then did you use a capital `E` in `-E 'say ... '`? – Borodin May 04 '16 at 04:36
  • Anyway, I have the information I need, and you have the same versions as I do. I have no idea why your code could possibly fail on your system while it works on mine. You will need to discover which statement is causing the error as I said. It may be that everything works as long as the login fails. – Borodin May 04 '16 at 04:38

1 Answers1

3

Your page is opening in some other language other than English. That's why you are getting that error. If you will open the page in English forcefully then the error will disappear. Try below address:

$mech->get( "https://en-gb.facebook.com/" );

or, you may click directly on instance of HTML::Form::SubmitInput obtained using this:

$mech->current_form()->find_input( undef, 'submit');

or, as there is only one click button in the form you can use click with no arguments.

$mech->click() 

or as suggested by @Borodin you can directly use(as the email and password field aren't translated):

$mech->submit_form( with_fields => { 
                        email => 'xxx@xxx.com', 
                        pass => 'xxxxxxx' 
                    }
                  );
Arunesh Singh
  • 3,489
  • 18
  • 26
  • 1
    *"Your page is opening in some other language other than English"* how did you diagnose that? – Borodin May 04 '16 at 06:24
  • I opened that page from India. It was opening in `Marathi`(local langauage in India) and I was getting same error as OP. – Arunesh Singh May 04 '16 at 06:25
  • So how does that cause *"Can't call method "header" on an undefined value"*? – Borodin May 04 '16 at 06:26
  • 1
    When the page opened in Marathi, The submit button became like this in form ``. So, there is no button with value `$mech->click_button(value => 'Log In');` to which OP is pointing. – Arunesh Singh May 04 '16 at 06:28
  • 1
    Ah I see! Yes, that makes sense. If I open `mr-in.facebook.com` then the submit button is called `लॉग इन`. The better way would probably be to use `$mech->submit_form( with_fields => { email => 'xxx@xxx.com', pass => 'xxxxxxx' })` as the `email` and `pass` field names aren't translated. Thanks for your explanation – Borodin May 04 '16 at 06:33
  • @Borodin, Yep Right. – Arunesh Singh May 04 '16 at 06:34
  • Thanks, it works now, I keep on testing with log for an hour to find out why only mine is not working – Zachary May 04 '16 at 06:45