Used WWW:Mechanize to login and download a file in perl script but cannot get into the actual page content

369 Views Asked by At

I am trying to login to the site using Mechanize in Perl script. It does log into the index page but I try to get the content of the page where file hyperlinks are present then I am not logged in I assume that after looking at the content of the page

Secondly, How can we download a file from the indirect hyperlink of a file which directs to some action which then downloads a file. Please guide, as there is not much help available for this particular use case

Below is my code

use WWW::Mechanize;
use LWP;

my $username = 'user'; 
my $password = 'pass';

my $mech = WWW::Mechanize->new();
$mech -> cookie_jar(HTTP::Cookies->new());
$mech -> get('some_website/index.html?#');
$mech -> form_name('form');
$mech -> field ('user' => $username);
$mech -> field ('password' => $password);
$mech->submit();
$mech -> get('actual_page_address_with_file_hyperlinks?adfgenDispatchAction=examine&idProgressivo=0&idFlusso=4200212');
#print $mech-> content();

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

my @links = $mech->links();
for my $link ( @links ) {

    #if(index($link->text,"Scarica")!= -1)
    #{
    printf "%s, %s\n", $link->text, $link->url;
    #}
}

;

1

There are 1 best solutions below

0
Hassan Munir On BEST ANSWER

As the website was using CSRF token and there was no hidden tag defined in the login page to store CSRF token, so token generation in the script was not helping.

I used WWW:: Selenium and I was able to log in to the site and download the files. Issue resolved –