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

Dear fellow monks, I need to write an app that opens an Internet Explorer browser and navigates to a certain URL, say www.google.com. Then, I should grab whatever text the user enters in the search form of the page. Is there a way to do that through Win32::OLE?
Reading http://www.perl.com/pub/a/2005/04/21/win32ole.html has helped me navigate to the requested URL and see the events printed in my screen but I haven't found info on how to get the user typed info after the click of the 'Search' button.
Any help would be appreciated!

use Tk; use Win32; use Win32::OLE qw(EVENTS); $mw = new MainWindow (-title => "Application to replicate user data fr +om web page"); # simple buttons to close IE or application $mw->geometry("350x100+200+200"); $mw->resizable(0,0); $customfont = "helvetica 10"; my $closeexlorerbutton = $mw -> Button(-text => "Close IE", -font => "fixed 10 bold", -height => "1", -width => "14", -command =>\&closeie) -> place(-x => 200, '-y' => 50); my $exitbutton = $mw -> Button(-text => "Cancel", -font => "fixed 10 bold", -height => "1", -width => "14", -command =>\&exitProgram) -> place(-x => 50, '-y' => 50); # open browser window and navigate to URL my $URL = "http://www.google.co.uk"; $IE = Win32::OLE->new("InternetExplorer.Application") || die "Could not start Internet Explorer.Application, Win32::OLE: +:LastError()\n"; Win32::OLE->WithEvents($IE,\&Event,"DWebBrowserEvents2"); $IE->{visible} = 1; $IE->Navigate($URL); Win32::OLE->MessageLoop(); MainLoop; sub exitProgram { my $response = $mw -> messageBox(-message=>"Are you sure you want +to exit?", -type=>'yesno', -icon=>'question'); if ( $response eq "yes" or $response eq "Yes" ) { exit; } }


Replies are listed 'Best First'.
Re: Grab text from web page using Win32::OLE
by ikegami (Patriarch) on Oct 10, 2008 at 14:09 UTC
      Hi,
      Thanks for the link. I had a look at the Win32::IE::Mechanize module but can not find much info apart from the CPAN page. I found people use the following piece of code to e.g. set a field in the page and submit it
      $ie->form_name( $name ) $ie->field($name,$value)
      However, some initial tests have failed.
      I tried to use $name and $field that I found in the source code of the page http://www.nationalrail.co.uk/times_fares/ldb/
      <form id="ldb" name="ldb" method="get" action="/times_fares/ldb/;jsess +ionid=10BE7B01E6848F6282BF503A24CD606F" onsubmit="kbSubmit(this)"> <div id="contentbg"> <h1><em>Live</em> Departures &amp; Arrivals</h1> <div class="jpbody"> <p>Please enter the entire, partial name or 3-letter c +ode for your station.</p> <fieldset> <div class="station"> <label for="mainStation">Station:</label> <input id="mainStation" name="mainStation" typ +e="text" value="" size="30"/> <div id="mainStationAc" class="sacDiv"></div>
      but it seems that the code snippet
      my $url = "http://www.nationalrail.co.uk/ldb/"; $ie = Win32::IE::Mechanize->new( visible => 1 ); $ie->get( $url ); my $formname = "ldb"; my $gotform = $ie->form_name( $formname );
      returns an undefined value for $gotform. And, of course, the same applies when I try to set a field, e.g. mainStation to a value.
      Furthermore, this is not exactly what I want to do. I want to get the user input when the user submits the form and not set it myself. Is this possible through Win32::IE::Mechanize?

      Sorry for so many questions, but I am a total newbie in working with perl + web forms.
      Athanasia
        An update on the previous.
        Working with another web page (in.yahoo.com) and adding a sleep 1 after the $ie->get($url) solved the problem of setting a value to a field and submitting the form correctly.
        However, the last question remains. Can I open a browser window through Win32::IE::Mechanize, then let the user type the input (e.g. some text for search) and grab this input in some way through this Perl module?

        Thanks in advance