Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Mechanizing around Javascript

by PerlSufi (Friar)
on Apr 25, 2013 at 20:43 UTC ( #1030732=perlquestion: print w/replies, xml ) Need Help??
PerlSufi has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I need to get to a page that has a report I need to download. I have been struggling with this for a couple days and I need some help. Here is my code:
UPDATE: When I run
$mech->get(' +tSearch.html'); print $mech->status();
I get a 200. Does that mean I can successfully retrieve my file located on that page? (with mechanize filling out the form stuff etc)
#! /usr/bin/perl #needs javascript navigation capability around line 34 -date 4/17/13 use WWW::Mechanize; use LWP::UserAgent; #This block of code follows from the CAPS homepage to the Account Inqu +iries link my $mech = WWW::Mechanize->new(); my $url = ""; $mech->get($url); if ($mech->success()){ print "Successful Connection\n"; } else { print "Not a successful connection\n"; } #This block of code is intended to fill in the required forms $mech->get($url); my $usr = "asdf"; #marked out for post my $pw = "asdffgg"; $mech->form_number(1); $mech->field( "login_name", $usr); $mech->form_number(2); $mech->field("user_password", $pw); $mech->click(); #follow link to dashboard $mech->follow_link( url=> ' +equestPage=P1DASHBOARD'); $mech->follow_link( url=> ' +do?group_name=Mailer&landing=Dashboard'); print $mech->content(); #my $ua = LWP::UserAgent->new(); #my $response = $ua->get(' +questPage=P1DASHBOARD'); #my $response2 = $ua->get(' +do?group_name=Mailer&landing=Dashboard'); #my $response3 = $ua->get(' +do?group_name=Mailer&landing=MailingReports'); #my $response4 = $ua->get(' +ashboard.html?reset=true'); #my $response5 = $ua->get (' +downloadAcsReportSearch.html'); #the last link above is the page I need to end up at. I commented this + out for now
Inspecting the page's java script has:
<script language="javascript"> function expendEvent(index) { if (document.getElementById("table"+ index).style.visible == 'hidd +en') { document.getElementById("table"+ index).style.visible = 'visib +le'; document.getElementById("image"+ index).src="images/hide_detai +ls.gif"; } else {document.getElementById("table"+ index).style.visible = 'hidden'; document.getElementById("image"+ index).src="images/show_details.g +if"; } if (document.getElementById("table"+ index).style.display == 'none +') {document.getElementById("table"+ index).style.display = 'inline'; } else { document.getElementById("table"+ index).style.display = 'none +';} } </script>
I get "link not found.." error when I run the script. Do I need to extract the tables that have the links?

Replies are listed 'Best First'.
Re: Mechanizing around Javascript
by runrig (Abbot) on Apr 25, 2013 at 22:26 UTC
    if ($mech->success()){
    The default is autocheck is ON, which means you don't have to check for success, it will die if unsuccessful.
    $mech->follow_link( url=> 'https://...
    Why follow_link? Just get() it.
Re: Mechanizing around Javascript
by Gangabass (Vicar) on Apr 26, 2013 at 12:26 UTC
    There is no form #2 on your login page. I recommend you to rewrite your code like this:
    $mech->get($url); my $usr = "asdf"; #marked out for post my $pw = "asdffgg"; $mech->submit_form ( with_fields => { login_name => $usr, user_password => $pw, }, );
    Also there is no need to use follow_link with absolute URL just use get
      Thanks. I have used the get() to receive my page, but I am still having problems getting te file by clicking a button in a form named "searchForm". It doesn't recognize the form id
        Please show HTML code you have in your browser for this page (usually you can get it by pressing Ctrl+U).

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2016-10-20 22:02 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (283 votes). Check out past polls.