Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get DOM from currently open IE browser

Status
Not open for further replies.

dadto7

Programmer
Dec 24, 2006
2
US
Basically what I need to do is get data from an already open IE window. I want to schedule the Perl script every minute to get the data that a user has entered in the browser window which never closes.

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 within the IE window. 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 Win32:OLE::GetActiveObject and EnumAllObjects methods and cannot find any Internet Explorer window open. I can find open Excel windows though. I saw some examples on the web but they all seem to open new IE windows and that does not help me. All the DOM stuff works if I open a new IE but I need to get to windows that are already open before my Perl script starts.

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.

Code:
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 are both Excel and IE windows open.

Code:
---- 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/Perl/osmerrors1.html?5,now.

I've seen references to using ShDocVw.dll but I'm not savvy enough to know how to call or find the methods in a DLL.

All help is greatly appreciated.
THX, Nick
 
Might be an idea to write an add-in for IE to dump the DOM to file?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Paul, that is a very good idea although I was hoping to avoid ActiveX. Because the Perl script runs every minute I'd have to contend with a potential busy file when IE writes the DOM (or just the data I need). I'm doing this temporarily but I'm looking for a better solution. In addition to learning more about Perl on Windows.

THX, Nick.
 
Writing the file to a temp location, and moving it when complete will avoid that issue.

Did you check out CPAN, there's a module called Win32::OLE-DOM which looks promising, though I've not used it, and can't see it on CPAN

Though this link refers to IE modules which use it

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top