in reply to How to control two tabs at the same time with WWW::Mechanize::Firefox

1. create two different WWW::Mechanize::Firefox objects one after the other. => seems to work (at least for opening the two pages in two different tabs), but i get this warning when i create the second browser:
Subroutine MozRepl::__load_plugins redefined at C:/strawberry/perl/sit +e/lib/Module/Pluggable/Fast.pm line 104, <DATA> line 1.

That warning is an unfortunate side-effect of Module::Pluggable::Fast, the plug-in system used by MozRepl. It is otherwise harmless.

2. get the current browser's Firefox::Application, open a new tab and select it. finally, try to use the Application's browser with Firefox::Application::browser(), and get() a page with it. => it gives me this output :
Use of uninitialized value in concatenation (.) or string at C:/strawb +erry/perl/site/lib/MozRepl/RemoteObject.pm line 835, <> line 3. MozRe +pl::RemoteObject: : Object has no function get at test_tabs_applicati +on.pl line 72.
I suppose it was simply not the right thing to do.

Exactly - a Firefox tab has vastly different methods from WWW::Mechanize(::Firefox). There should be a way to construct another WWW::Mechanize::Firefox object by supplying a freshly connected tab, but that is no option yet. Maybe in a later version, but as you can simply create another WWW::Mechanize::Firefox object, it is no high priority.

Replies are listed 'Best First'.
Re^2: How to control two tabs at the same time with WWW::Mechanize::Firefox
by mascip (Pilgrim) on Mar 11, 2012 at 19:33 UTC

    Thank you !

    I have one last small question (shall i ask it in another node ?) :
    When i load my pages, it takes some time. Most of the time, i don't need to wait for the images to be loaded in the page, just the HTML content, and sometimes the DOM.
    I found an imperfect solution, and am looking for the a better one.
    At the moment i use

    synchronize => 0,
    in the WWW::Mechanize::Firefox constructor, and then wait 1 second. But sometimes, it is not enough. Sometimes, 5 seconds doesn't seem to be enough either.

    I think i need to use the "events" parameter of the WWW:Mechanize::Firefox contructor. For example

    events => ['DOMContentLoaded'],
    (as 'DOMContentLoaded' doesn't wait for the images to be loaded, contrarely to 'load').
    It still seems to be slow.
    Is that the best solution ?

    I am sorry, i have read on this subject several time in the past, and have searched again today. I am making you repeat. The best solution i read seems to be

    events => ['DOMContentLoaded'],
    but it seems fairly slow, so i thought...i can try to ask again, maybe i missed something.

      I'm not aware of a faster method than waiting for the DOMContentLoaded event. You can speed things up if you only need to wait for certain elements of the page becoming available, but if you want the DOM to be complete, I know of no better event to wait for.

        Wait for one particular element, yes that could be it.
        * time passes and i search *
        I found it !
        I had seen it several weeks ago, but had not understood at the time, and then forgotten.
        In the cookbook, HERE.

        Here is my version, and it doesn't work. When i call the WWW::Mechanize::Firefox::is_visible() subroutine, it stops in the middle and says "10 elements found for '//a@class="kl"' at test_events_synchronize.pl line 51."
        Apparently this happens in find_link_dom(), and it highlights many elements in the window. What am i doing wrong? Here is my code :

        #!/usr/bin/perl use 5.012003; # Perl version requirement use strict; use warnings; #comment before releasing use autodie 2.10; use Carp 1.25; use Perl6::Say 0.16; use Readonly 1.03; use Scalar::Util qw( dualvar ); # ##### use WWW::Mechanize::Firefox 0.59; use Time::HiRes 1.9721 qw( time ); # where is my Firefox executable ? Readonly my $FIREFOX_FOLDER => 'C:\Program Files\Mozilla Firefox\firefox.exe'; # ###### # URL address of the two pages that we want to open in two tabs Readonly my $URL => 'https://www.google.ca/search?tbm=isch&hl=en&sourc +e=hp&biw=1400&bih=866&q=big+images&gbv=2&oq=big+images&aq=f&aqi=g10&a +ql=&gs_sm=3&gs_upl=1564l3028l0l3402l10l10l0l0l0l0l142l1032l4.6l10l0&g +s_l=img.3..0l10.1564l3028l0l3403l10l10l0l0l0l0l142l1032l4j6l10l0'; # start time my $t_start = time; # start browser 1 my $browser = WWW::Mechanize::Firefox->new( # launch Firefox if it is not already running launch => $FIREFOX_FOLDER, # events to wait for, before continuing after get()ing a page # events => ['DOMContentLoaded'], synchronize => 0, ); croak 'Could not initialize the browser' if !$browser->success(); say '# Browser is now ready to go'; # open page $browser->get( $URL ); croak 'Could not open the page' if !$browser->success(); say '# Page opened'; my $test_to_execute = 1; if( $test_to_execute == 1 ) { # wait until one particular element appears my $retries = 10; while ($retries-- && ! $browser->is_visible( xpath => '//a[@class="kl"]' )) { sleep 1; }; die "Timeout" unless $retries; } elsif( $test_to_execute == 2 ) { my $test_is_visible = $browser->is_visible( xpath => '//a[@class=" +kl"]' ); say $test_is_visible; } # print time passed my $t_passed = dualvar time - $t_start, 'time passed'; say "$t_passed = " . ($t_passed+0); # stop the program, so i can check which pages have been opened say '# done !'; my $stop = <ARGV>

        PS : if is set

        $test_to_execute = 2
        i get exactly the same result.