Morning, O Multisplendiferous Monks of Much Munging! I humbly come before thee with a question...

So I'm trying out WWW::Mechanize::Firefox. Very, very cool module - the author ought to be decorated and lauded to the skies. Wow. Awesome. I'm having lots of fun, and doing things with it I never thought you could do on the Web - jumpin' JavaScript, I can mechanize JS-generated logins, too cool for words!

But, there's one piece of it that I just can't figure out. Here's the setup: I want to be able to log into a site (yep, I have the credentials), grab a generated string off that first page, then go back and repeat the process 6 more times. However, since it takes a second or two to log in - or more, depending on the connection - the process occasionally fails because I tried to grab the content too fast. Here's what my code looks like right now:

#!/usr/bin/perl -w use strict; die "Usage: ", $0 =~ /([^\/]+)$/, " <URL>\n" unless @ARGV; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(); $mech->get($ARGV[0]); $mech->activateTab(); my($user, $pass) = qw{SHHH_its_a_secret Yeah_sure_whatever}; for (1..7){ $mech->submit_form( with_fields => { username => $user, password => $pass, } ); sleep 1; if ($mech->content =~ /of the day is <br>(.*)/){ print "$_ => '$1'\n"; } $mech->back; }

What I'd really like to do is replace that 'sleep 1' with some sort of a gadget that tells me that the login went through - i.e., the page finished loading - and so now I can go ahead and scrape it. The docs show a 'progress_listener' method that's supposed to "set up the callbacks for the ``nsIWebProgressListener'' interface to be the Perl subroutines you pass in" - but honest to goodness, I must be too stupid to figure it out. I've tried setting it up just the way it's shown in the docs, with the only difference being the sub that I call:

my $eventlistener = $mech -> progress_listener( $browser, onLocationChange => \&process_content, );

where 'process_content' is just that last 'if' block, and it does nothing - no errors, no warnings, just no output. Never gets called, as far as I can tell.

So, any advice on setting up this callback thing - or maybe an explanation of why it's not the right thing, and what the right thing is - would be really appreciated! Thanks to all for listening, and have a great POSIX::strftime("%A",localtime) !


In reply to WWW::Mechanize::Firefox - callbacks? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.