Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: WWW:Mechanize::Chrome incognito

by Discipulus (Canon)
on May 23, 2020 at 11:00 UTC ( [id://11117156]=note: print w/replies, xml ) Need Help??


in reply to WWW:Mechanize::Chrome incognito

Hello tuneroaster and weclome to the monastery!

incognito => 0 option has be proven to be currently broken as Corion said (he is the author an, yes! he will fix it ;) also in a very recent post by me: some doubts on my first steps with WWW::Mechanize::Chrome and remember Super Search is our friend!

> please elaborate

If the chrome browser was started without options atm incognito will be always be the mode. But if you start chrome with the option --remote-debugging-port=9222 or manually or in your perl program, then you'll be able to connect to an existing instance and to an existing tab. I was prepaering the following example that shows what I mean:

use strict; use warnings; use Log::Log4perl qw(:easy); use WWW::Mechanize::Chrome; Log::Log4perl->easy_init($ERROR); # Set priority of root logger to ER +ROR my $mech = connect_to_an_existing_tab(); unless ( $mech ){ my $chrome_exe = choose_chrome_executable(); start_new_instance( $chrome_exe ) } print "broswer started or connected succesfully, listening on remote-d +ebugging-port ". ${$mech->target()}{transport}{port}, " status ", ${$mech->target()}{transport}{is_connected} ? "connected" : + "not connected","\n"; sleep 3; # go from Monastery Gates to Super Search $mech->click ( { selector => '#titlebar-top tbody tr td.monktitleb +ar ul li:nth-child(16) a'} ); # the super search form $mech->form_number( 2 ); # titles containing text field $mech->field( 'HIT' => 'WWW::Mechanize::Chrome'); # not working: SUPPORT NEEDED HERE ;) # # $mech->untick( 're', 'S' ); # $mech->tick( 're', 'N' ); # submit $mech->click( { selector => '#id-3989 center table tbody tr td.main_co +ntent form p:nth-child(17) input[type=submit]'} ); # ATTENTION: autoclose was fixed recently: # if ( $WWW::Mechanize::Chrome::VERSION < 0.56){ print "Press ENTER to terminte ( autoclose => 0 fixed in version 0 +.56 of the module)".."\n"; <STDIN>; } sub choose_chrome_executable{ my( $chrome, $diagnosis ) = WWW::Mechanize::Chrome->find_executabl +e(); if ( $diagnosis ){ print "problem finding chrome executable: $diagnosis\n"; } print "Should I use ",( $chrome ? "[$chrome]" : "-NOT FOUND-" ), " + or another executable (put full path or leave blank for default)\n"; my $chrome_path = <STDIN>; chomp $chrome_path; $chrome_path = -e $chrome_path ? $chrome_path : $chrome; return $chrome_path; } sub connect_to_an_existing_tab{ print "I have to use an existing chrome tab by title( for example +PerlMonks )? leave it blank to open a new browser instance\n"; my $tab_title = <STDIN>; chomp $tab_title; if ( $tab_title ){ my $mech; { local $@; eval{ $mech = WWW::Mechanize::Chrome->new( mute_audio => 0, autoclose => 0, autodie => 0, incognito => 0, tab => qr/$tab_title/, ); }; if ( $@ ){ print "problem connecting to an already open tab in a +running instance of chrome:\n$@\n\n". "close your running chrome instance and anothe +r one will be started with right configuration\n". "Press ENTER to continue..\n"; <STDIN>; return 0; } return $mech; } } else { return 0 } } sub start_new_instance{ my $chrome_exe = shift; print "I'll now open https://www.perlmonks.org (will be in incogn +ito mode despite incognito => 0)\n"; my $url = 'https://www.perlmonks.org'; $mech = WWW::Mechanize::Chrome->new( mute_audio => 0, autoclose => 0, autodie => 0, incognito => 0, launch_exe => $chrome_exe, launch_arg => [ "--remote-debugging-port=9222" ] ); $mech->get( $url ); }

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: WWW:Mechanize::Chrome incognito
by tunerooster (Acolyte) on May 23, 2020 at 14:25 UTC

    STOP THE PRESSES! IT WORKED:

    From the shell, I failed to start chromium with --remote-debugging-port=9222

    So I guess, in a script, I can spawn chromium headless, then run the per script and attach to it! It just might work! THANKS!

    ---------------------------------------

    OK, my code is now:
    use Log::Log4perl qw(:easy); use WWW::Mechanize::Chrome; Log::Log4perl->easy_init($ERROR); my $mech = WWW::Mechanize::Chrome->new( headless => 0, # start_url => 'https://grocery.walmart.com/orders', data_directory => '/home/rwk/chrome-session-for-walmart', incognito => 0, launch_exe => '/usr/bin/chromium', javascript => 1, launch_arg => [ "--remote-debugging-port=9222" ], tab => qr/Walmart Grocery/, ); my $txt = $mech->content; open($f, '>', '/tmp/out.html'); print $f $txt; close $f;
    I start the browser manually and load https://grocery.walmart.com/orders

    It correctly logs in and display my order page.

    The page Title shows "Walmart Grocery".

    I run my script above and get:

    error when connectingInternal Exception at /usr/local/lib64/perl5/5.30 +.1/WWW/Mechanize/Chrome.pm line 993.
    Please advise...
Re^2: WWW:Mechanize::Chrome incognito
by tunerooster (Acolyte) on May 23, 2020 at 11:48 UTC
    Thanks again! You say:

    "if you start chrome with the option --remote-debugging-port=9222 or manually or in your perl program, then you'll be able to connect to an existing instance and to an existing tab"

    Does this mean my task cannot be scripted? I.e, I must run chrome from a shell, then attach to it?

    Do you also have to set "port => 9222" in the perl constructor?

Re^2: WWW:Mechanize::Chrome incognito
by tunerooster (Acolyte) on May 23, 2020 at 12:14 UTC
    I tried your script, got the fillowing:
    I'll now open https://www.perlmonks.org (will be in incognito mode de +spite incognito => 0) error when connectingInternal Exception at /usr/local/lib64/perl5/5.30 +.1/WWW/Mechanize/Chrome.pm line 993. at /usr/local/lib64/perl5/5.30.1/Future.pm line 899. Future::result(Future=HASH(0x55e2e584d3c0)) called at /usr/local/l +ib64/perl5/5.30.1/Future.pm line 926 Future::get(Future=HASH(0x55e2e584d3c0)) called at /usr/local/lib6 +4/perl5/5.30.1/WWW/Mechanize/Chrome.pm line 993 WWW::Mechanize::Chrome::_connect(WWW::Mechanize::Chrome=HASH(0x55e +2e4ab30d0), "mute_audio", 0, "pid", 17465, "launch_exe", "/usr/bin/ch +romium", "writer_fh", ...) called at /usr/local/lib64/perl5/5.30.1/WW +W/Mechanize/Chrome.pm line 966 WWW::Mechanize::Chrome::new("WWW::Mechanize::Chrome", "mute_audio" +, 0, "autoclose", 0, "autodie", 0, "incognito", ...) called at /home/ +rwk/mech-sample.pl line 99 main::start_new_instance("/usr/bin/chromium") called at /home/rwk/ +mech-sample.pl line 14

    I am using WMC 0.56

    BTW, Thanks for all your work on this!!!! WMC appears to be VERY useful (once I get it working).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found