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

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.

Replies are listed 'Best First'.
Re^3: How to control two tabs at the same time with WWW::Mechanize::Firefox
by Corion (Patriarch) on Mar 11, 2012 at 20:28 UTC

    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.

        ->is_visible expects your XPath expression to find 0 or 1 element, not more. If you know that there will be zero or multiple elements, you can restrict your XPath expression by using an index:

        //a[@class="kl"][1]