http://qs321.pair.com?node_id=560974

bcdeery has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to click an unnamed button while using Mechanize. The HTML is:
<input type="button" name="" value="Get Detailed +Quote" onclick="goToPage('goToPackageInfoPage')" class="buttonpurple"> <input type="button" name="" value="Get Quick Quot +e" onclick="goToPage('getQuickQuote')" class="buttonpurple"> <input type="button" name="" value="Get Transit Ti +me" onclick="setTTOn();goToPage('goToPackageInfoPage')" class="buttonpurple">
I'm trying to click the third button, but have no idea how. I've tried $mech->click_button( value=>'Get Transit Time'); but I just get the error "Can't call method "header" on an undefined value at C:\...Mechanize.pm line 1868, <MYFILE> line 1. Any ideas?

Replies are listed 'Best First'.
Re: Mechanize clicking unnamed button
by marto (Cardinal) on Jul 13, 2006 at 15:09 UTC
    bcdeery,

    Though it is considered (by some) rude to answer a question with another question, are you using WWW::Mechanize or Win32::IE::Mecahnize? The reason I ask is that click that button calls the JavaScript function goToPage, and WWW::Mechanize does not support JavaScript.

    However to answer your question you can use the $mech->click_button method to click the nth button such as $mech->click_button(number=>3);. You may want to check out what the JavaScript does, it is perhaps possible to achieve what it is doing using Perl. Remember if in doubt read the documentation. Also, you have shown us the snippit of the HTML in question, but not your Perl code that is throwning the error.

    Update: Slight change of formatting.

    Hope this helps.

    Martin
      Update: I'm using WWW::Mechanize. Other than Java support, is there much difference in the two? Here is my code:
      #set initital webpage $url="http://www.fedex.com/ratefinder/home?cc=US&language=en&link=1&li +d=//Ship//Pack+Rates+Corp"; #open initial webpage my $mech = WWW::Mechanize->new(); $mech->get($url); while($line=<ZIPPAIRS>){ $indexForm2 = "standAloneActionForm"; $mech->form_name($indexForm2) ; chomp $line; ($Ozip, $Dzip)= split("\t",$line); print " $Dzip $Ozip\n"; #enter the zip codes and package weight of 3 lbs $mech->set_fields( origZip => $Ozip, destZip => $Dzip, totalPackageWeight =>"3", ); $mech->click_button(number=>3); #print current URL's html code to file 2 my $results_html = $mech->content; print OUTFILEB $results_html; die "All Done.\n";
      nothing prints to the file. I had $mech->click_button( value=>'Get Transit Time'); in where you had suggested to try using the button number. When I try the button number, I get an error "Can't call method "click" on an undefined value..... Sorry, I'm not very good with the boards. I have only used them a few times and haven't been on in over 6 months.
        bcdeery,

        I answered a similar question you asked a while back, explaining how to reverse engineer JavaScript. A quick look at the Win32::IE::Mechanize documentation will explain what the differences are between WWW::Mechanize and Win32::IE::Mechanize. Your post history seems to centre around the area of site scraping and processing. A quick look at your target site, in this case www.fedex.com suggests that you may want to check out Section 2. Use of fedex.com. from their Terms of service, which is something you may want to consider:

        "The use of automated dial-in or inquiry devices to obtain information through fedex.com is strictly prohibited."

        For this reason I won't be providing any more information over and above what has already been provided to you.

        Thanks

        Martin
Re: Mechanize clicking unnamed button
by reneeb (Chaplain) on Jul 13, 2006 at 15:59 UTC
    As WWW::Mechanize cannot handle JavaScript, I would recommend to use LiveHTTPHeaders for Firefox to analyze the request and do the requests yourself.