Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Just another Perl shrine
 
PerlMonks  

Follow-up Javascript Hyperlink

by Samy_rio (Vicar)
on Nov 04, 2006 at 06:29 UTC ( #582216=perlquestion: print w/ replies, xml ) Need Help??
Samy_rio has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have tried to automate the browsing method using WWW::Mechanize module as below:

use strict; use Data::Dumper; use WWW::Mechanize; use POSIX qw(strftime); my $currSysDate =strftime ("%b".'&year='."%y", localtime); # DAY Langu +age my $mech = WWW::Mechanize->new(autocheck => 1); my $url = "http://www.somenet.com/login.aspx"; my $user = 'user'; my $pass = 'pass'; $mech->get( $url ); if ($mech->success()) { $mech->submit_form( form_name => 'Form1', fields => { txtUsername => $user, txtPassword => $pass, }, button => 'Button3', ); $mech->get("http://www.somenet.com/showCardEmpMonthlyReport.aspx? +month=$currSysDate&Reptype=&empcode=$user"); my $str = $mech->content(); print $str; }

I am getting error as:

Error GETing javascript:__doPostBack('EmpSideMenu1$lnkAttendance',''): + Protocol scheme 'javascript' is not supported at array.pl line 10047

Source of particular URL is:

<TD class="sidemenu" style="BACKGROUND-IMAGE: url(Images1\MenuBg.jpg); + BACKGROUND-REPEAT: repeat-x">&nbsp;&nbsp;&nbsp; <a id="EmpSideMenu1_lnkAttendance" href="javascript:__doPostBack('EmpS +ideMenu1$lnkAttendance','')" style="color:White;width:160px;">Attenda +nce Report</a></TD> <script language="javascript"> <!-- function __doPostBack(eventTarget, eventArgument) { var theform; if (window.navigator.appName.toLowerCase().indexOf("netscape") + > -1) { theform = document.forms["Form1"]; } else { theform = document.Form1; } theform.__EVENTTARGET.value = eventTarget.split("$").join(":") +; theform.__EVENTARGUMENT.value = eventArgument; theform.submit(); } // --> </script>

From chatterbox, I came to know WWW::Mechanize doesn't support Javascript. Based on Corion suggestion I have used Win32::IE::Mechanize module. Eventhen javascript hyper-link is not working in follow_link.

use strict; use Data::Dumper; use Win32::IE::Mechanize; my $ie = Win32::IE::Mechanize->new( visible => 1 ); my $url = "http://www.somenet.com/login.aspx"; my $user = 'user'; my $pass = 'pass'; $ie->get( $url ); if ($ie->success()) { $ie->submit_form( form_name => 'Form1', fields => { txtUsername => $user, txtPassword => $pass, }, button => 'Button3', ); $ie->follow_link( text_regex => qr/Attendance Report/i ); #Not wo +rking $ie->submit_form( form_name => 'Form1', button => 'Button1', ); print $ie->content(); } __END__ Some time Error as: Can't locate object method "warn" via package "sssself" (perhaps you f +orgot to load "sssself"?) at C:/Perl/site/lib/Win32/IE/Mechanize.pm l +ine 971.

How to follow up the javascript hyper-link? Please help me. Thanks in advance.

Updated I have updated sssself->warn( "No inputcontrol by the name '$fname'" ); as $self->warn( "No inputcontrol by the name '$fname'" ); in Win32::IE::Mechanize. Still not working.

Regards,
Velusamy R.


eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Comment on Follow-up Javascript Hyperlink
Select or Download Code
Re: Follow-up Javascript Hyperlink
by perrin (Chancellor) on Nov 04, 2006 at 17:27 UTC
    All you have to do is look at what the JavaScript does and do it yourself in Perl. The result of the JavaScript function will be just an ordinary HTTP interaction that Mechanize can do with no trouble.
Re: Follow-up Javascript Hyperlink
by Anonymous Monk on Jul 13, 2007 at 04:26 UTC
    I created this routine to deal with this situation and do cut/paste/modify from javascript to code
    sub doPostBack { my $agent = shift; ## WWW::Mechanize agent-object passed in my $target = shift; ## first argument in the __doPostBack() cal +l in javascript my $arg = shift; ## second argument in the __doPostBack() ca +ll in javascript # convert the passed in string $target =~ s/\$/:/g; $agent->form_number(1); $agent->field('__EVENTTARGET', $target); $agent->field('__EVENTARGUMENT', $arg); $agent->submit(); } #endsub doPostBack # function __doPostBack(eventTarget, eventArgument) { # var theform; # if (window.navigator.appName.toLowerCase().indexOf("microsoft") > + -1) { # theform = document.MTMMAIN; # } # else { # theform = document.forms["MTMMAIN"]; # } # theform.__EVENTTARGET.value = eventTarget.split("$").join(":"); # theform.__EVENTARGUMENT.value = eventArgument; # theform.submit(); # }
      Thanks for your routine!!! Has been really useful!!
      That explained a LOT of things -- Thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://582216]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2014-04-16 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (413 votes), past polls