Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

using perl to open internet explorer browser

Posted on 2003-10-30
13
Medium Priority
?
1,400 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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
 
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 300 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans

718 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