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

Mechanizing Javascript form

by Roy Johnson (Monsignor)
on Nov 10, 2003 at 17:34 UTC ( [id://305920]=perlquestion: print w/replies, xml ) Need Help??

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

Since I'm confined to ActivePerl, it appears I have to do without WWW::Mechanize. I mention this because I'm hoping someone will say that I'm wrong.

Be that as it may, the more daunting issue I have to deal with is that of simulating the submission of a Javascript form (i.e., it's an HTML form, but the Submit button executes Javascript rather than posting the form). This seems, to me, to be beyond the scope of the ordinary LWP and friends. I'm thinking that I need a program that can interact with an actual browser or something.

But I want to be wrong. Does anyone see a Path of Least Pain? Fundamentally, all I am really trying to do is to ensure that some services are reachable and functioning (this includes a "Secure Web Zone Login" that appears to be somewhat magical).

Replies are listed 'Best First'.
Re: Mechanizing Javascript form
by Corion (Patriarch) on Nov 10, 2003 at 17:39 UTC

    I'm quite sure that WWW::Mechanize works under ActiveState Perl. What did you try and how did it fail?

    Automating JavaScript is best done by looking at the JavaScript and then programming the same stuff in Perl to recreate what the JavaScript does. A good thing to look at is what the browser finally sends over to the server.

    If you're hell-bent on automating a browser, take a look at HTML::Display, which automates IE to display HTML.

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
      I'm quite sure that WWW::Mechanize works under ActiveState Perl. What did you try and how did it fail?
      I looked at http://ppm.activestate.com/BuildStatus/5.8-W.html where it lists Mechanize as a FAIL and unavailable. My proxy iswas failing to connect under PPM, so I haven't been able to test it directly but I've fixed the problem and installed Mechanize. Huzzah!
      Automating JavaScript is best done by looking at the JavaScript and then programming the same stuff in Perl to recreate what the JavaScript does. A good thing to look at is what the browser finally sends over to the server.
      That's almost surely the path I will be taking. I posted because it would be more elegant to "simply" execute the code. I couldn't find a module that said it could do that, but sometimes I miss things. Often, really. After all, there is a Javascript module, and a Mechanize module, so it isn't unimaginable...

      Thanks for your response.

        To successfully automate JavaScript, you not only need a working JavaScript virtual machine (as JavaScript.pm and Scripting::JavaScript provide, you also need the HTML DOM plus some other objects like frame, window and other weirdness, which is possible to implement, but if your only goal is to automate some web interaction, it's most likely easier to simply do the task at hand than to solve the generic problem.

        perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: Mechanizing Javascript form
by jsprat (Curate) on Nov 10, 2003 at 18:20 UTC
    It is definitely available for AS:

    Mon 11/10/2003 10:11:25.94 C:\>ppm search WWW-Mechanize Searching in Active Repositories 1. WWW-Mechanize [0.37] (none) 2. WWW-Mechanize [0.40] (none) 3. WWW-Mechanize [0.43] (none) 4. WWW-Mechanize [0.44] (none) 5. WWW-Mechanize [0.48] (none) 6. WWW-Mechanize [0.48] automate interaction with websit +es

    perldoc ppm or read A Guide to Installing Modules for more detailed instructions on installing modules.

    Good luck!

Re: Mechanizing Javascript form
by perrin (Chancellor) on Nov 10, 2003 at 18:27 UTC
    Most JavaScript hardly does anything at all as far as the server is concerned, and the server is the only thing that matters here. Just look at the script, figure out what it does (change a form value? submit another form?) and then write Perl code that does the same thing. In many cases, the JavaScript can be ignored completely.
      In this case, the Javascript is the whole purpose of the page. Or it seems to be. I haven't fully deciphered it, but the form itself has no action attribute. It takes username and password and (somehow) authenticates the intranet user for the session.

      I have to get my session authenticated so I can check that protected documents are being served. It promises to be tedious, but that's not Perl's fault.

        Forms can exist and function without an action attribute.

        Show us the page, why don't you, and let us figure out what it's doing?

        The fundamental point is that a JavaScript form almost certainly doesn't do anything that an equivalent HTML form can do. The JS probably just checks that certain fields aren't empty or something like that -- checks the buttons/fields before submitting. After submitting, it's just another form.



        ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-20 02:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found