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

Driving Internet Explorer with Perl

by zejames (Hermit)
on Sep 23, 2004 at 16:22 UTC ( [id://393252]=CUFP: print w/replies, xml ) Need Help??

The following code can be used to script Internet Explorer actions. The exemple chosen is here is to fill in a form and then validate it through a perl script.

For this test script, let's imagine we have a HTML form named 'log_form' (<form ... name='log_form'>), a login input named 'login' ((<input type='text' name='login'>), a password input named 'pass' and a submit button named 'go'.

To find more information about how to drive IE, have a look here : http://msdn.microsoft.com/workshop/browser/webbrowser/reference/objects/internetexplorer.asp
use Win32::OLE qw( EVENTS in with valof ); use Win32::OLE::Variant; Win32::OLE->Option( Warn => 0 ); $HomePage = "http://www.greyhats.org/new/"; $IE = Win32::OLE->GetActiveObject( 'InternetExplorer.Application' ); if( ! defined $IE ) { print "Can not find open object. Creating one...\n"; $IE = Win32::OLE->new( 'InternetExplorer.Application', "Quit" ) or die "Unable to create a IE Object\n"; } Win32::OLE->WithEvents( $IE, \&cleanExit, 'DWebBrowserEvents2' ); $IE->{Visible} = 1; $IE->{RegisterAsDropTarget} = 1; $IE->{RegisterAsBrowser} = 1; $IE->Navigate( $HomePage ); # Let IE load the page while( $IE->{Busy} ) { while ($IE->SpinMessageLoop()) { select undef,undef,undef,0.25; } } $Doc = $IE->{Document}; $forms = $Doc->{forms} or die "Unable to retrieve forms"; $form = $forms->item("log_form") or die "Unable to find my form"; $elements = $form->{elements} or die "Can't get the elements"; $login = $elements->item("login",0) or die "Where is my login !?"; $pass = $elements->item("pass", 0) or die "I wonder why there isn't a +pass"; $go = $elements->item("go", 0) or die "I want my button"; $login->{value} = "zejames"; $pass->{value} = "test"; $go->click; # Do whatever you want. sub cleanExit { my( $obj, $event, @args ) = @_; if ($event eq "OnQuit") { print "Terminating\n"; undef $obj; exit(1); } }

Replies are listed 'Best First'.
Re: Driving Internet Explorer with Perl
by tachyon (Chancellor) on Sep 24, 2004 at 04:31 UTC

    Chill on the system resources with a 250 msec sleep while we wait.

    while( $IE->{Busy} ) { while ($IE->SpinMessageLoop()) { select undef,undef,undef,0.25 } }

    cheers

    tachyon

      Hi Tachyon, Thanks for the script. I still get error that "unable to find my form ". Any clue?
Re: Driving Internet Explorer with Perl
by McMahon (Chaplain) on Sep 23, 2004 at 17:48 UTC
    Also take a look at samie
      Is there any similar module for driving mozilla/netscape, say on linux? Thanks
Re: Driving Internet Explorer with Perl
by johnnywang (Priest) on Sep 23, 2004 at 23:20 UTC
    I'm getting the "Unable to find my form" error. I'm sure I will be able to use this code. Thanks.
Re: Driving Internet Explorer with Perl
by Anonymous Monk on Nov 05, 2004 at 16:55 UTC
    This post was very helpful! Thanks! If I already have several Internet Explorer windows open, how can I instruct Perl which one to latch on to? I could check all IE windows for the URL or target I want, but I don't know how to scan through all of the open IE windows. Thanks in advance, Mike
      my $shell = Win32::OLE->new('Shell.Application'); my @ies = grep { $_->FullName =~ /iexplore\.exe$/ } in $shell->Windows +; foreach $ie (@ies){# eval{ $ie->{document}->getElementById("PUT THE ID OF AN ELEMENT THAT + IDENTIFIES THE PAGE YOU'RE LOOKING FOR HERE"); $IEWindowYouAreLookingFor = $ie; };#end eval }#END FOREACH
      The way it works, if the element you're looking for isn't there the call to getElementById throws an error and the line:

      $IEWindowYouAreLookingFor = $ie;

      never executes. Otherewise the line runs and $IEWindowYouAreLookingFor gets set to point to the $ie window you're looking for. Use the Microsoft IE Developer toolbar to track down the IDs of stuff on the page.

      This doesn't work if the end user has mulitple pages from the same site up. I don't know any way to check if the page has focus...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found