-2

I am new to Perl, so as an exercise I have been trying to log in to a web page and open the logged-in page later from the command line.

This is the code I wrote

use HTTP::Cookies;

use lib "/home/tivo/Desktop/exp/WWW-Mechanize-1.80/lib/WWW";
use Mechanize;

$mech = WWW::Mechanize->new();
$mech->cookie_jar(HTTP::Cookies->new());

$url = "<url>";

$mech->credentials('username' => 'password');
$mech->get($url);

$mech->save_content("logged_in.html");

After I execute the script, I try to open the saved HTML page using the command

$ firefox logged_in.html

But I get the error

BIG-IP can not find session information in the request. This can happen because your browser restarted after an add-on was installed. If this occurred, click the link below to continue. This can also happen because cookies are disabled in your browser. If so, enable cookies in your browser and start a new session.

The same code worked for Facebook login.

Borodin
  • 126,100
  • 9
  • 70
  • 144
Satzoda
  • 1
  • 1

1 Answers1

0

Here are the main issues

  • You haven't installed WWW::Mechanize; it looks like you've just downloaded it and unpacked it, and added use lib to point to where it's unpacked. You need to run cpan WWW::Mechanize from the command line to install it properly, and then it will also be in a directory where Perl looks for libraries by default so there will be no need for a use lib at all

  • You need to use WWW::Mechanize. Just use Mechanize won't do

  • You must always start every Perl program with use strict and use warnings 'all', and declare all your variables with my

Fixing those should get you a long way towards working

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • I tried to install the WWW::Mechanize package from the cpan command line, however it always failed. So, I downloaded the tar file to a local directory extracted it and installed it manually using the following commands MakeFile.PL, make, make test and make install. As for the the scope of variables, I am still experimenting with the code once it gets fixed I l add the use strict and use warnings @Borodin – Satzoda Sep 27 '16 at 06:58
  • Okay. I was waiting for you to post the "following commands"! – Borodin Sep 27 '16 at 07:03
  • Sorry for that..I edited the comment..Take a look. @Borodin – Satzoda Sep 27 '16 at 07:10
  • In that case you have the module installed but you're still using the distribution package. Take out `use lib` altogether and fix `use WWW::Mechanize`. And you have it backwards for `use strict` and `use warnings 'all'`: they will help you develop your program by revealing simple bugs in your code. – Borodin Sep 27 '16 at 07:14
  • Okay..I will include the use strict and use warnings and declare the variables as my. As for the fixing of use WWW::Mechanize, I am lost as I have tried all the possible ways to the best of my knowledge (which might be inadequate). Kindly suggest on how to proceed. @Borodin – Satzoda Sep 27 '16 at 07:26
  • It's very simple. Just `use WWW::Mechanize`. If that doesn't work then you haven't installed it properly. – Borodin Sep 27 '16 at 07:28