perlfirefoxmechanizewww-mechanize

WWW::Mechanize::Firefox Timeout


How do I properly use a timeout when I attempt to make ->get(URL) requests with WWW::Mechanize::Firefox?

my $mech = WWW::Mechanize::Firefox->new(timeout => 10); does not seem to work


Solution

  • It is possible to simulate this, at least to a good extent.

    You can turn off synchronization for get, in which case the call should return immediately. Then poll every $sleep_time until timeout, with some test of whether the page completed. The sleep allows all those other good pages to complete, so set $sleep_time as appropriate.

    my $timeout = 10; 
    my $sleep_time = 1;
    
    my $page = get($url, synchronize => 0); 
    
    for (1..$timeout) {
        # Test some page property that will confirm that it loaded
        last if $page->title eq $expected_title;
        sleep $sleep_time;
    }
    

    There is the issue of how exactly to confirm each page, but this should provide a working timeout.