Solved

using perl to open internet explorer browser

Posted on 2003-10-30
13
1,163 Views
Last Modified: 2008-02-01
I am trying to run a script that feeds urls to a browser window, and then actually with help of ps -ef finding pid for internet explorer so I can kill it.

Any hints?  

thanks
0
Comment
Question by:RG2DM
  • 4
  • 3
  • 3
  • +1
13 Comments
 
LVL 20

Expert Comment

by:jmcg
ID: 9662045
I can't answer your question, but if you respond to my clarification request, some of the other experts may be able to solve it.

Normally, 'ps' is a command that is run on a UNIX or Linux system -- a place where Internet Explorer does not run. If you're on a Windows system, I believe you'd use Win32::OLE module calls to start up an instance of IE, direct it to various URLs, and finally shut it down.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9662926
system('iexplorer.exe your-URL');
0
 

Author Comment

by:RG2DM
ID: 9673440
jmcg,

I actually installed mks toolkit, which lets you utilize some unix commands on a Windows machine.  ps does work, but maybe not correctly through a script ?!?

ahoffmann,

using that syntax doesn't work the way i want, because that does open a browser with the url, but the script will wait until the browser is closed before it proceeds with the rest of the script.  
0
 

Author Comment

by:RG2DM
ID: 9679753
jmcg,

I have uninstalled MKS Toolkit, to be able to utilize Win32::OLE properly.  
I did get a browser open by :

$browser = Win32::OLE->new('InternetExplorer.Application') or die "Can't start IE";

$browser->Navigate('http://www.google.com', 1,'_BLANK');

Now my question is, how do you close the browser?

I will increase the points, since you got me in the right direction but there is still the window to close completely
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9679861
Win32::OLE might be the M$-way to do it (I don't know)
but you may also use fork() and then start system("iexplorer.exe URL") in the child.
0
 

Author Comment

by:RG2DM
ID: 9680524
ok.  instead of win32::ole, I use fork.
Looking at the books that I have, and the listings I see under fork, i am confused.

Tried an example but the sequence doesn't match or makes sense.  Can you help me a little?

#! /usr/bin/perl -w

# Parent Process

print "Starting the Parent Process!\n";

unless ($pid = fork)
      {
      #Start of Child Process
      for (1..10)
            {
            system "iexplore.exe www.google.com";            
            sleep 1;
            }
            exit 0;
      }

# End of Child Process

# Continuation of Parent
for (1..5)
      {
      print "Parent, Count $_\n";
      sleep 2;
      }
      
waitpid ($pid, 0);

# End of Parent
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 11

Expert Comment

by:turn123
ID: 9681237
You can close IE when your done with it buy calling it like this.

my $BROWSER = Win32::OLE->new('InternetExplorer.Application', 'Quit');

Taken from this post by Douglas Galbraith
http://groups.yahoo.com/group/perl-win32-users/message/24591?source=1
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9682443
and you are aware that your child opens 10 Explorers one after each other?
You parent loop (for(1..5)) is useless, somehow ..
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9684301
I guess the group here has fewer Windows users than I thought.

The post that Turn123 pointed you to has the elements that I expected would be the way to do what you want, thought the one line he quoted is only sufficient to get a handle on a not-yet-displayed InternetExplorer window. It's a start, not an end.

So the sequence I just tested out with ActivePerl looks like this:

use Win32::OLE;
use Win32::OLE::Const;

my $Browser = Win32::OLE->new( 'InternetExplorer.Application', 'Quit');
$Browser->{'Visible'} = 1;
$Browser->Navigate( 'http://www.experts-exchange.com/');

sleep 30;

$Browser->Quit;

===============

You can test the $Browser->{Busy} flag to check whether it has finished retrieving and displaying the latest Navigate command.

I think I have a use for this code already. I'm so glad you asked this question and that Turn123 has pointed at such a valuable answer.

Ahoffman's fork suggestion is another way to go, but I think it limits you to sending a kill signal to the $pid of the child process as a way of stopping the process.

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9685976
as said, I only know perl, not M$, but I believe in perl's modules and if there is one for M$ it might be a better solution than using traditional fork()
I'm learning too, so I keep jmcg's sample
;-)
0
 
LVL 11

Accepted Solution

by:
turn123 earned 75 total points
ID: 9687853
I finished the script I was working on when I found the answer on how to close it and will post it here.

It works on my XP computer.

It is a package so you would need to add this line in the calling script "require orders::webfetch;"
To get a value you would need to set $site to your website and $visible to 0 to hide or 1 to see IExplorer.

I named it webfetch.pm

It is called like this.  my $rawdata = orders::webfetch::fetch ($site, $visible);

##      This opens a Internet Explorer window, downloads the contents, then closes it.

use Win32::OLE;
use Win32::OLE::Const;
package orders::webfetch;
sub fetch {
      my $BROWSER = Win32::OLE->new('InternetExplorer.Application', 'QUIT');
      # access the web site
      $BROWSER->{'Visible'}=$_[1];
      $BROWSER->Navigate("$_[0]");
      
      # wait until HTML page has loaded
      do
      {
            sleep(1);
            $stat=$BROWSER->{Busy};
      }
      while ($stat);

      # get the HTML page from the browser
      my $body=$BROWSER->Document->{body};
      my $html = $body->{innerHTML};
      
      return $html;
}

return 1;

I hope this helps.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9688037
Turn123,

Is there a reason you're using IE to fetch rather than fetching the HTML directly with LWP?

You're not calling $BROWSER->Quit in your routine, so I assume that the IE process goes away when your sub returns and the $BROWSER object goes out-of-scope?
0
 
LVL 11

Expert Comment

by:turn123
ID: 9688650
I'm logging into a site that uses an SSL connection and requires a username and password so this was the eaisest way I found to do it.

I'm not sure about $BROWSER->Quit and didn't think about it.  I've added it.  Thanks for pointing it out.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now