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

Manually, in my chrome browser, when I visit grocery.walmart.com, it remembers my login and logs me in automatically. When I access the site within WWW::Mechanize::Chrome, it does not. I am pretty sure Walmart uses cookies to do this. I simply need to know how to make auto-login via cookies work with WWW::Mechanize::Chrome. This is my code which does not auto-login:

use Log::Log4perl qw(:easy); use WWW::Mechanize::Chrome; Log::Log4perl->easy_init($ERROR); my $mech = WWW::Mechanize::Chrome->new( headless => 0, ); $mech->get('https://grocery.walmart.com'); my $png = $mech->content_as_png();

I should mention, it is critical to auto-login because Walmart uses login captchas to defeat bots.

Thanks for any help!

P.S. Any idea why WWW::Mechanize::Chrome doesn't load cookies by default?

Replies are listed 'Best First'.
Re: WWW:Mechanize::Chrome cookies
by Corion (Patriarch) on May 21, 2020 at 17:45 UTC

    Currently, WMC uses a fresh/separate profile always. You can create a persistent profile by using the data_directory option:

    my $mech = WWW::Mechanize::Chrome->new( headless => 0, data_directory => '/home/tunerooster/chrome-session-for-walmart', );

    This lets Chrome keep cookies between invocations.

      Great! That almost works... But, it opens two (2) chrome windows, one in "incognito" mode at "grocery.walmart.com" and one chrome window *not* in incognito mode (regular mode?) at "about:blank". Although the docs say incognito defaults to false, I set it to 0 (zero) anyway but it makes no difference and the docs say nothing more regarding incognito. I can manually close the walmart page (which, btw, is not reading the login cookies) then go to walmart in the non-incognito window and YES, it then works. However, I need to make it open walmart in one non-incognito window automatically, and I should be good to go...

      Any additional help is greatly appreciated!

      My code now:

      use Log::Log4perl qw(:easy); use WWW::Mechanize::Chrome; Log::Log4perl->easy_init($ERROR); # Set priority of root logger to ER +ROR my $mech = WWW::Mechanize::Chrome->new( headless => 0, data_directory => '/home/rwk/chrome-session-for-walmart', incognito => 0, ); $mech->get('https://grocery.walmart.com'); my $png = $mech->content_as_png();

      P.S. How do I select between the two open chrome windows? I can't find it anywhere in the docs...

        With version 0.58 , the second window should not open anymore unless you specify separate_session in the constructor.

        Also, it should make the cookies available that are stored in the data_directory.

Re: WWW:Mechanize::Chrome cookies
by nysus (Parson) on May 21, 2020 at 17:11 UTC
      I am using 0.55 (from a cpan installed from a few days ago), but I see 0.56 is apparently just released. I updated to 0.56 but it makes no difference (see other reply above). Thanks!