On Tue, Feb 10, 2015 at 5:10 AM, Gary Stainburn < gary.stainb...@ringways.co.uk> wrote:
> Hi folks, > > I'm porting a WWW::Mechanize based program to WWW::Mechanize::Firefox > because > the web site I access is now much more Javascript based and maintenance is > getting to be a nightmare. > > However, I'm struggling on the basics. Yesterday I started off with a very > basic program. I wrote this last night and it worked fine, logging into the > web site. > > This morning it no longer works, returning from the get() before the page > is > complete, which then means that the form fields don't exist. I didn't > change > the script, just shut my laptop down overnight. > > Anyone got any ideas? > > If I add a sleep() it works fine. > > > #!/usr/bin/perl -w > use strict; > use WWW::Mechanize::Firefox; > > my $mech = WWW::Mechanize::Firefox->new( > activate => 1 # bring the tab to the foreground > ); > $mech->autoclose_tab( 0 ); > print "getting login page\n"; > $mech->get('https://www.xxxxxxxx.com/'); > print "get complete, got status :",$mech->status(),"\n"; > #sleep(5); > $mech->form_name('logonForm'); > print "form name=",$mech->current_form->{name},"\n"; > $mech->field(userid=>'xxxxxxx'); > $mech->field(Pwd=>'xxxxxx'); > $mech->submit; > > > sleep(999) Hi Gary, I had a project a while back that I converted for similar reasons as you pointed out. (Javascript based page) I found the following to work for me and included it in your example with comments. #!/usr/bin/perl use strict; use warnings; # I prefer this method I learned via Shlomi use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new( activate => 1 # bring the tab to the foreground ); $mech->autoclose_tab( 0 ); print "getting login page\n"; $mech->get('https://www.xxxxxxxx.com/'); sleep(5); # Give the page time to load and for firefox on your local computer time to "execute" the javascript on the page print "get complete, got status :",$mech->status(),"\n"; $mech->clear_js_errors(); # Clear any JS errors. $mech->stack_depth(0); # Added so that a local cache version is not kept. Fresh version for each download. #$mech->autoclose_tab; # uncomment only if you used "$mech->autoclose_tab(1)" above so that the tab you open is now closed. $mech->form_name('logonForm'); print "form name=",$mech->current_form->{ > > name},"\n"; > $mech->field(userid=>'xxxxxxx'); > $mech->field(Pwd=>'xxxxxx'); > $mech->submit; > > > sleep(999) ## END OF CODE -Chappy