package Win32::OLE::IE; use strict; use warnings; use Win32::OLE qw(EVENTS); use Time::HiRes qw(sleep); sub new { my ($class, %args) = @_; my $self = {}; bless $self, $class; $self->{IE} = Win32::OLE->new("InternetExplorer.Application") or d +ie $!; my %defaults = ( left => 0, top => 0, height => 400, width => 700, menubar => 1, toolbar => 1, statusbar => 1, visible => 1, %args ); $self->{IE}->{$_} = $defaults{$_} foreach keys %defaults; $self->pause; return $self; } sub content { my ($self) = @_; my $doc = $self->{IE}->{Document}; return $doc->{DocumentElement}->{InnerHTML} if defined($doc->{DocumentElement}); } sub navigate { my ($self, $url) = @_; $self->{IE}->navigate($url); $self->pause(); } sub messageloop { my ($self) = @_; Win32::OLE->MessageLoop(); } sub pause { my ($self) = @_; while ($self->{IE}->{Busy} == 1) { sleep(0.2); } } sub DESTROY { my ($self) = @_; $self->{IE}->Quit(); } sub setform { my ($self, $name, $value, $index) = @_; $index = 0 if !$index; my $cnt = 0; my $doc = $self->{IE}->{Document}; my $forms = $doc->forms; for (my $i = 0 ; $i < $forms->length ; $i++) { my $form = $forms->item($i); for (my $j = 0 ; $j < $form->elements->all->length ; $j++) { my $elemname = $form->elements->all($j)->getAttribute("nam +e"); if ($elemname and $elemname eq $name) { if ($index == $cnt) { $form->elements->all($j)->{value} = $value; return; } else { $cnt++; } } } } die "could not find name=$name index=$index"; } sub pushbutton { my ($self, $name, $index) = @_; $index = 0 if !$index; my $cnt = 0; my $doc = $self->{IE}->{Document}; my $forms = $doc->forms; for (my $i = 0 ; $i < $forms->length ; $i++) { my $form = $forms->item($i); for (my $j = 0 ; $j < $form->elements->all->length ; $j++) { my $elemname = $form->elements->all($j)->getAttribute("nam +e"); my $elemtype = $form->elements->all($j)->getAttribute("typ +e"); if ( $elemname and $elemtype and $elemname eq $name and $elemtype eq 'submit') { if ($index == $cnt) { $form->elements->all($j)->click; $self->pause; return; } else { $cnt++; } } } } die "could not find name=$name index=$index"; } sub clicklink { my ($self, $name, $index) = @_; $index = 0 if !$index; my $cnt = 0; my $doc = $self->{IE}->{Document}; for (my $i = 0 ; $i < $doc->all->length ; $i++) { if ($doc->all($i)->tagName =~ /^A$/) { my $text = $doc->all($i)->innerText; if ($text and $text =~ /\s*\Q$name\E\s*$/) { if ($index == $cnt) { $doc->all($i)->click(); $doc->all($i)->fireEvent("onclick"); $self->pause; return; } else { $cnt++; } } } } die "could not find name=$name index=$index"; } 1; __DATA__ =head1 NAME OLE::IE - Perl extension for automating Internet Explorer =head1 SYNOPSIS use OLE::IE; my $ie = Win32::OLE::IE->new; $ie->navigate("http://www.google.com"); my $html = $ie->content; $ie->setform('q','university of massachusetts', 0); $ie->pushbutton('btnG', 0); $ie->clicklink('Advanced Search',0); =head1 DESCRIPTION An OLE wrapper for Internet Explorer, loosely based on Win32::SAM. =head1 SEE ALSO Win32::SAM

In reply to OLE wrapper for IE by rkg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":