in reply to Re^2: 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

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.

  • Comment on Re^3: How to control two tabs at the same time with WWW::Mechanize::Firefox
  • Download Code

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

    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]

        It doesn't work if i use

        xpath => '//a[@class="kl"][1]'
        (i get exactly the same error).
        But it works when i use
        xpath => '//input[@class="gbqfif"]'
        which is a unique element in the page.
        The result is perfect : 2 seconds or less instead of 5 or 6! Thank you so much!

        I hope you're having good times :o)

        PS: i forgot to say yesterday that in the cookbook (here) the code is (slighly) broken:
        When $retries reaches 0, then it becomes equal to -1 and the "Timeout" exception is not triggered.
        It's just a detail, but i thought it might help others if it was fixed : i waited for 10 seconds many times before realizing that is was not the page charging, but just a Timeout.