Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Need help with WWW::Mechanize and Chrome cookies

by bakiperl (Beadle)
on Jul 08, 2021 at 13:22 UTC ( [id://11134796]=perlquestion: print w/replies, xml ) Need Help??

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

Can WWW::Mechanize load google chrome cookies?

I am using the following code but it doesn't seem to work. The saved credentials don't load.

use HTTP::Cookies; use HTTP::Cookies::Chrome; use WWW::Mechanize; my $cookie_file = 'C:/Users/someuser/AppData/Local/Google/Chrome/User +Data/Default/Cookies'; my $cookie_jar = HTTP::Cookies::Chrome->new( file => $cookie_file, ); my $mech = WWW::Mechanize->new( cookie_jar => $cookie_jar, ); my $uri = URI->new( "" ); my $username = 'user'; my $passwd = 'pass'; $mech->get( $uri ); $mech->set_fields( username => $username, password => $passwd, ); $mech -> submit();

On the other hand, I am able to get WWW::Mechanize::chrome to work using the following code but I prefer WWW::Mechanize because WWW::Mechanize::chrome is giving me a hard time downloading hyperlinked text files and images.

use HTTP::Cookies; use Log::Log4perl qw(:easy); use WWW::Mechanize::Chrome; my $cookie_dir = 'C:/Users/someuser/AppData/Local/Google/Chrome/User D +ata/Default/'; my $mech = WWW::Mechanize::Chrome->new( data_directory => $cookie_dir, ); my $uri = URI->new( "" ); my $username = 'user'; my $passwd = 'pass'; $mech->get( $uri ); $mech->set_fields( username => $username, password => $passwd, ); $mech -> submit();
Please help.

Replies are listed 'Best First'.
Re: Need help with WWW::Mechanize and Chrome cookies
by jo37 (Deacon) on Jul 08, 2021 at 13:37 UTC

    I don't fully understand your problem and thus cannot tell if it is relevant, but the value of $cookie_dir differs in your examples.


      The first example uses the cookie file while in the second one uses the directory where the cookie resides.

      The code that uses WWW::Mechanize::Chrome loads the credentials from the cookie and allows the login but the code that uses the WWW::Mechanize does not. That's the problem.

Re: Need help with WWW::Mechanize and Chrome cookies
by Anonymous Monk on Jul 09, 2021 at 09:25 UTC
    How are you checking that they load?
      The WMC works interactively with the chrome browser. The login info is loaded from the cookie and the target page is reached.
        I also wonder why this code in WMC
        my $file_map = $mech->saveResources_future( target_file => 'this_page.html', target_dir => 'this_page_files/', wanted => sub { $_[0]->{url} =~ m!^https?:!i }, )->get();
        downloads the files in these two links
        <link rel="stylesheet" href="css/file.css" type="text/css" /> <img id="logo" src="/images/image.gif" alt="" title="" />
        But not this one
        <a class="txt" href="file.txt"> Text File </a>
        Heh. You said this is busted
        WWW::Mechanize->new( cookie_jar => $cookie_jar, );

        you said its not picking up cookie and sending it. But the code does no checking of sent cookies. So how?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11134796]
Approved by haukex
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-07-22 11:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.