Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

How to get DOM from currently open IE browser.

by dadto7 (Novice)
on Nov 06, 2007 at 21:05 UTC ( #649341=perlquestion: print w/replies, xml ) Need Help??
dadto7 has asked for the wisdom of the Perl Monks concerning the following question:

Basically what I need to do is get data from an already open IE window. I want to run the Perl script every minute to get the data that a user has entered in the browser window.

I can find the browser windows using Win32::Gui & GuiTest but from my searches it looks like I need to use Win32::OLE to extract anything from the windows. I'm hoping to fiddle with the DOM to get what I need from the web page and possibly change values later on.

So I've tried the GetActiveObject and EnumAllObjects and cannot find any Internet Explorer window although I can find Excel windows. I saw some examples on the web but they all seem to open new IE windows and that does not help me. I need to get to windows that are already open.

I'm using IE 6.0.29 on XP SP2 and Perl V5.8.8. Here is some of the code that does not work for me.

use strict; use warnings; use Cwd; use Win32::Gui; use Win32::Clipboard; use Win32::GuiTest qw(:ALL); # Had to use GuiTest to do a sendkeys. Also found ShowWind-minimize me +thod (don't use hide or you will never see the window again) use Win32::OLE; # need this to open up and IE window use Win32::OLE qw(EVENTS in with valof ); use Win32::OLE::Variant; Win32::OLE->Option( Warn => 2 ); # Always warn with verbose error mess +ages # try to find all OLE objects my $Count = Win32::OLE->EnumAllObjects(sub { my $Object = shift; my $Class = Win32::OLE->QueryObjectType($Object); printf "# Object=%s Class=%s\n", $Object, $Class; }); print "---- The count of all OLE objects is '$Count'.\n"; my $loExcel; $loExcel = Win32::OLE->GetActiveObject('Excel.Application'); if ( defined $loExcel ) { print "Found Excel.\n"; } else { print "Did not find and Excel object. LastError=", Win32::OLE->LastError, "\n"}; my $IE; $IE = Win32::OLE->GetActiveObject( "InternetExplorer.Application" ); print "=== GetActiveObject for ie returns '$IE'\n\n" if ( defined $IE) +; if ( !defined $IE ) { print "Can not find open IE object. Creating one...\n"; # $IE = Win32::OLE->new( 'InternetExplorer.Application', "Quit" +) $IE = Win32::OLE->new( "InternetExplorer.Application" ) or die "Unable to create a IE Object\n"; ################ need logic in here to wait for window to open # +############################# # Win32::OLE->WithEvents($IE,\&Event,"DWebBrowserEvents2"); }
This is the output I get when there is an Excel and IE window open.
---- The count of all OLE objects is '0'. Found Excel. Can not find open IE object. Creating one... The current directory is H:/src/Perl. Url found from IE object 'file://H:\src\Perl\osmerrors1.html' is=file: +//H:/src/P erl/osmerrors1.html?5,now.
Any help is greatly appreciated. I've spent many hours with the google monster and haven't found anything that works yet. So I now have admit I'm over my head and ask for help. THX, Nick

Replies are listed 'Best First'.
Re: How to get DOM from currently open IE browser.
by Joost (Canon) on Nov 06, 2007 at 21:38 UTC
Re: How to get DOM from currently open IE browser.
by technojosh (Priest) on Nov 07, 2007 at 11:36 UTC
    Since you:
    use Win32::GuiTest qw(:ALL);

    take a look at its methods:

    FindWindowLike(); SetActiveWindow(); SetForegroundWindow();

    CPAN Link

    The above link has much better examples, but in a nutshell 'FindWindowLike' will find the IE window by name (among other things), and give you its ID (among other things). You can use that with 'SetActiveWindow' in combination with 'SetForegroundWindow' to grab your IE window and give it focus.

      Thanks for your reply and the link is great. I've used some of that before where I can get things from various windows and use the Clipboard to manipulate the data.

      Unfortunately, I do not see how this can help be get things out of the HTML/DOM. Let's say I want to get the value from a SPAN id named "Errorcnt". In JavaScript it would look something like "document.getElementById('Errorcnt').innerHTML". This is why I feel that I'll have to use the Win32::OLE package stuff.

      Did you have a Win32:GuiTest method in mind that would do that?

      THX, Nick.

        I saw some examples on the web but they all seem to open new IE windows and that does not help me. I need to get to windows that are already open.

        My suggestions were for your above issue, those methods I mentioned would help to get you to an IE window that is already open...I apologize if I misread your question.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://649341]
Approved by ikegami
Front-paged by Old_Gray_Bear
and the rats come out to play...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2016-12-10 12:17 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (162 votes). Check out past polls.