Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Need help with WWW::Mechanize and Chrome cookies

by bakiperl (Beadle)
on Jul 08, 2021 at 13:22 UTC ( #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( "https://www.something.com" ); 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( "https://www.something.com" ); 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 (Chaplain) 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.

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
      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.
        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?

        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>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2023-03-30 02:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (73 votes). Check out past polls.

    Notices?