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

Hi, Im trying to automate a website using WWW::Mechanize module. In this process I encoutered with an error. Here is the code snippet I used
use strict; use WWW::Mechanize; #use HTTP::Cookies; my $outfile = "out.htm"; my $url = "www.abc.com"; my $username = "usname"; my $password = "passwd"; my $mech = WWW::Mechanize->new(); $mech->get($url); $mech->field(userName => $username); $mech->field(password => $password); $mech->click("button");# Button in first page #Traversed to second page $mech->follow_link("alink");#Successfully completed $mech->click("Button2");#Error my $output_page = $mech->content(); open(OUTFILE, ">$outfile"); print OUTFILE "$output_page"; close(OUTFILE);
Using this code I was able to do login and then I follwed the link "alink", when I tried clicking the button after getting the new page(after the link "alink" is pressed) its thowing an error saying that "U MIGHT CLICKED MULTIPLE BUTTONS AT A TIME" and page is logging of... Here is the html code snippet, I got after following the link "alink":
<input type="button2" name="Button2" value="Hello World" onclick="retu +rn incrementClicks('14');" class="buttonSubmit"> </TD><td>&nbsp;&nbsp;</td> <td id="hideReset"><input type="reset" name="reset" value="Reset" o +nclick="return clearFields();" class="buttonSubmit"> </td>
Can anyone please help me in this regard. Thanks in Advance --R

Replies are listed 'Best First'.
Re: Error while using WWW::Mechanize
by ikegami (Patriarch) on Dec 30, 2010 at 09:47 UTC

    onclick="return incrementClicks('14');"

    WWW::Mechanize doesn't have a JS interpreter. You'll either have to do the equivalent of the JS yourself, or switch to something that processes JS. As for the later, I think there's a module that adds some measure of JS to WWW::Mechanize (can't remember the name), and there's a couple of other Mechanize modules that use JS-aware backends (e.g. IE, Mozilla).

      Also see WWW::Scripter, which has a Javascript emulator as backend but doesn't conveniently show up in a "Mechanize" search. I haven't used it myself as I didn't have the necessity of headlessly automating a Javascript-laden website.

      Thanks a lot for the help