Hi,
I playing with IE automation. Many scripts in this newsgroup show code like
the following to open a web page and wait for its completion:

Set oIEApp = CreateObject("InternetExplorer.Application")
With oIEApp
.Visible = True
.Resizable = True
.FullScreen = False
.Height = 600 : .Width = 600
.Top = 50 : .Left = 100
.Navigate("msdn.microsoft.com")
Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
MsgBox "Done navigating to msdn.microsoft.com"
End With

Other scripts listen for the DocumentComplete event. If I count the
occurences of the DocumentComplete event, I find that my 'done' message box
appears after the DocumentComplete event has fired twice, and that the
DocumentComplete event fires once more, perhaps right when
msdn.microsoft.com is truely done loading. When just starting IE and
navigating to one URL, waiting for a ReadyState of 4 is sufficient.

Many of us are not satisfied with navigating to only one URL; we want to go
more places. But the ReadyState remains 4 for a hundred or more millisecons
after specifying a new URL, then changes to 3, and finally changes back to 4
when the page has loaded. So we need logic like this for the second URL:

Option Explicit
Dim oIEApp
Set oIEApp = CreateObject("InternetExplorer.Application")
With oIEApp
.Visible = True
.Resizable = True
.FullScreen = False
.Height = 600 : .Width = 600
.Top = 50 : .Left = 100
.Navigate("msdn.microsoft.com")
Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
MsgBox "Done navigating to msdn.microsoft.com"
.Navigate("aol.com")
Do While.ReadyState = 4 : WScript.Sleep 2 : Loop
Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
MsgBox "Done navigating to aol.com"
End With

With this script, most of the time the AOL done message occurs when the page
really is loaded. Occasionally, the AOL done message occurs long before the
page really is loaded, especially when CPU usage is high.

From this I have concluded that neither ReadyState nor the DocumentComplete
event are reliable indicators of a web page's being complete.

I've monitored how often various events occur by the time the MsgBoxes
appear, and I'm currently using the following logic to determine completion.


Thanks,
-Paul Randall

Re: IE automation - how to tell when document is complete? by Csaba

Csaba
Thu Jul 21 15:40:17 CDT 2005

Paul Randall wrote:
> I've monitored how often various events occur by the time the MsgBoxes
> appear, and I'm currently using the following logic to determine completion.
>
>
> Thanks,
> -Paul Randall

Thanks for a very interesting post. But don't keep me hanging Paul,
what is that following logic?!? In my reading, nothing followed...

By the way, one possible (not exactly satisfying) solution (which I'm
using, but not in your context) is to use the CreateObject with events
(2nd argument), and then trap for the DownloadComplete event (as
opposed to looping till it changes). Now the problem with this, as
you've pointed out, is that it fires multiple times and ready state
could be any of several values. For my purposes, by the time
readyState is 3, it's enough to have me muck with the DOM of the loaded
page. So I set a semaphore within the DOM itself (so that if I have
another DownloadComplete firing I will ignore it if I see the
semaphore).

Regards,
Csaba Gabor from Vienna