Sorry, I accidently hit send.

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.

Option Explicit
Dim oIEApp
Dim iCount_BeforeNavigate2, iCount_NavigateComplete2
Set oIEApp = WScript.CreateObject("InternetExplorer.Application", "event_")
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
Do Until iCount_BeforeNavigate2 = iCount_NavigateComplete2
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
Do Until iCount_BeforeNavigate2 = iCount_NavigateComplete2
WScript.Sleep 100
Loop
MsgBox "Done navigating to aol.com"
End With

Sub event_BeforeNavigate2(objBrowser, strURL, Flags, _
TargetFrameName, PostData, Headers, bCancel)
iCount_BeforeNavigate2 = iCount_BeforeNavigate2 + 1
End Sub

sub event_NavigateComplete2(objBrowser, strURL)
iCount_NavigateComplete2 = iCount_NavigateComplete2 + 1
End Sub

This seems like overkill. Does anyone have a simpler way to reliably detect
document complete?

Thanks,
-Paul Randall

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

Paul
Sat Jul 23 20:35:21 CDT 2005

Two things:
1)
Someone kindly deleted a message I had screwed up and sent accidently, which
someone else had replied to, so it was deleted too. But Google found it:

Csaba Gabor Jul 21, 2:40 pm show options

Newsgroups: microsoft.public.scripting.vbscript
From: "Csaba Gabor" <C...@z6.com> - Find messages by this author
Date: 21 Jul 2005 13:40:17 -0700
Local: Thurs,Jul 21 2005 2:40 pm
Subject: Re: IE automation - how to tell when document is
complete?
Reply | Reply to Author | Forward | Print | Individual Message |
Show original | Report Abuse



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



2)
Thefollowing code doesn't always work. I have found at least one web site
where
iCount_BeforeNavigate2 consistently exceeds iCount_NavigateComplete2 long
after the web page is completely loaded. Perhaps I will try Csaba's
suggestion to use a readyState of 3. On the website I had problems with,
iCount_BeforeNavigate2 consistently exceeded iCount_NavigateComplete2 by
exactly one, so it was easy to make a special case test.
...
.Navigate("aol.com")
Do While.ReadyState = 4 : WScript.Sleep 2 : Loop
Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
Do Until iCount_BeforeNavigate2 = iCount_NavigateComplete2
WScript.Sleep 100
Loop
MsgBox "Done navigating to aol.
...

My current position is that there is no way for VBScript to know whether a
web page has completely loaded. Hopefully someone will post code to prove
me wrong.

-Paul Randall


begin 666 dot_clear.gif
K1TE&.#EA`0`!`( ``/___P```"'Y! $`````+ `````!``$```("1 $`.P``
`
end


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

Csaba
Sun Jul 24 02:32:23 CDT 2005

Csaba Gabor wrote:
> 1) 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).

Paul Randall wrote:
> 2)
> Thefollowing code doesn't always work. I have found at least one web site
> where
> iCount_BeforeNavigate2 consistently exceeds iCount_NavigateComplete2 long
> after the web page is completely loaded. Perhaps I will try Csaba's
> suggestion to use a readyState of 3. On the website I had problems with,
> iCount_BeforeNavigate2 consistently exceeded iCount_NavigateComplete2 by
> exactly one, so it was easy to make a special case test.
> ...
> .Navigate("aol.com")
> Do While.ReadyState = 4 : WScript.Sleep 2 : Loop
> Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
> Do Until iCount_BeforeNavigate2 = iCount_NavigateComplete2
> WScript.Sleep 100
> Loop
> MsgBox "Done navigating to aol.
> ...
>
> My current position is that there is no way for VBScript to know whether a
> web page has completely loaded. Hopefully someone will post code to prove
> me wrong.

Google is my filing cabinet.

Your solution seemed on track with my my approach so I didn't comment.
However, I was wondering about matching up those counts. Frankly, this
whole document done loading thing seems rather finicky.

Some thoughts:
My method seems to work with about 99% accuracy. Although a 1% failure
rate is high (as far as I'm concerned), IE itself seems to fail that
frequently (e.g. Hit a submit button and it needs to be submitted
again, etc.)

I'm actually checking for readyState being 2 (and not 3). That was an
error in my description above. 2 indicates that you have access to the
DOM. Note, however, that this check is being done within the
IE_DocumentComplete firing.

What does it mean for a document to be done loading? Does it mean (No)
that all the pictures are also loaded? Does it mean (I forget on
I/Frame loadings) that all the ads are loaded? What about (Yes) the
<SCRIPT src=...> elements. I've encountered pages (e.g. credit card
payment ones) that can take over a minute to load. One way that I used
to do what you are doing (serial page navigation), when I was running
nightly scripts (hence had time to burn) was to do the navigate and
then wait for 90 seconds. If the page hadn't loaded by then I just
gave up and reported an error.

An idea for that .readyState is not being reset concomitantly with the
.Navigate is to possibly intersperse distinct pages with a .Navigate2
to "about:blank". The intention here is that IE should be really quick
to clear stuff out on an "about:blank" page. Or not. Might be worth
experimenting with. Not a very hearwarming approach, though.


One idea which I had but did not test (and actually, I'm quite curious
about how effective it would be) is that when that documentComplete
fires the first time with a readyState of at least 2, to commandeer the
IE.Document.body.onload with something like:

IE.Document.parentWindow.execScript _
"var tmpLoad=document.body.onload ? document.body.onload : null; " & _
"document.body.onload = function(oldLoad) {return function() { " & _
"alert('proof of usurpation'); " & vbcrlf & _
"if (oldLoad) oldLoad(); } } (tmpLoad); "


Injecting a <SCRIPT src=...> element (unless one does fancy tricks)
seems to cause IE to claim that it does not find the page (even when it
is already being shown in front of your nose). I'm guessing this is
some misguided attempt at security (I say misguided since IE has
already given access to its DOM and is running code on behalf of
vbscript and has said yes to mucking about with its DOM. Furthermore,
it is possible to bypass this nuissance by manually injecting the
script).

Can you detect, and what should your response be if the F5 (refresh
key) is hit - but this point is moot in your approach, which is to
iterate through a fixed sequence of pages.

Csaba Gabor from Vienna


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

mayayana
Sun Jul 24 17:18:18 CDT 2005

I'm not sure if this will provide the solution, but it
might be worth testing. In VB, using a Web Browser
object that is essentially the same as IE, I use the
DocumentComplete event and check the document
object. It seems to be dependable, as far as I can tell.
Something like:

Sub IE_DocumentComplete()
if Not IE.document Is Nothing Then .....

--
--
Paul Randall <paulr901@cableone.net> wrote in message
news:u0hBiR$jFHA.576@tk2msftngp13.phx.gbl...
> Two things:
> 1)
> Someone kindly deleted a message I had screwed up and sent accidently,
which
> someone else had replied to, so it was deleted too. But Google found it:
>
> Csaba Gabor Jul 21, 2:40 pm show options
>
> Newsgroups: microsoft.public.scripting.vbscript
> From: "Csaba Gabor" <C...@z6.com> - Find messages by this
author
> Date: 21 Jul 2005 13:40:17 -0700
> Local: Thurs,Jul 21 2005 2:40 pm
> Subject: Re: IE automation - how to tell when document is
> complete?
> Reply | Reply to Author | Forward | Print | Individual Message
|
> Show original | Report Abuse
>
>
>
> 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
>
>
>
> 2)
> Thefollowing code doesn't always work. I have found at least one web site
> where
> iCount_BeforeNavigate2 consistently exceeds iCount_NavigateComplete2 long
> after the web page is completely loaded. Perhaps I will try Csaba's
> suggestion to use a readyState of 3. On the website I had problems with,
> iCount_BeforeNavigate2 consistently exceeded iCount_NavigateComplete2 by
> exactly one, so it was easy to make a special case test.
> ...
> .Navigate("aol.com")
> Do While.ReadyState = 4 : WScript.Sleep 2 : Loop
> Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
> Do Until iCount_BeforeNavigate2 = iCount_NavigateComplete2
> WScript.Sleep 100
> Loop
> MsgBox "Done navigating to aol.
> ...
>
> My current position is that there is no way for VBScript to know whether a
> web page has completely loaded. Hopefully someone will post code to prove
> me wrong.
>
> -Paul Randall
>
>
>



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

Paul
Mon Jul 25 23:06:25 CDT 2005

Thanks Csaba and mayayana
I will try the ideas when I have time. For now, I just use a special case
for web pages with unequal values for iCount_BeforeNavigate2 and
iCount_NavigateComplete2.

Since web page loading is asynchronous to the script, trying to do something
with
"if Not IE.document Is Nothing Then ....." is likely to fail since the
status of IE.document could change while "then ....." is processing.

-Paul Randall


"mayayana" <mayaXXyana1a@mindYYspring.com> wrote in message
news:KsUEe.16533$aY6.2548@newsread1.news.atl.earthlink.net...
> I'm not sure if this will provide the solution, but it
> might be worth testing. In VB, using a Web Browser
> object that is essentially the same as IE, I use the
> DocumentComplete event and check the document
> object. It seems to be dependable, as far as I can tell.
> Something like:
>
> Sub IE_DocumentComplete()
> if Not IE.document Is Nothing Then .....
>
> --
> --
> Paul Randall <paulr901@cableone.net> wrote in message
> news:u0hBiR$jFHA.576@tk2msftngp13.phx.gbl...
> > Two things:
> > 1)
> > Someone kindly deleted a message I had screwed up and sent accidently,
> which
> > someone else had replied to, so it was deleted too. But Google found
it:
> >
> > Csaba Gabor Jul 21, 2:40 pm show options
> >
> > Newsgroups: microsoft.public.scripting.vbscript
> > From: "Csaba Gabor" <C...@z6.com> - Find messages by this
> author
> > Date: 21 Jul 2005 13:40:17 -0700
> > Local: Thurs,Jul 21 2005 2:40 pm
> > Subject: Re: IE automation - how to tell when document is
> > complete?
> > Reply | Reply to Author | Forward | Print | Individual
Message
> |
> > Show original | Report Abuse
> >
> >
> >
> > 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
> >
> >
> >
> > 2)
> > Thefollowing code doesn't always work. I have found at least one web
site
> > where
> > iCount_BeforeNavigate2 consistently exceeds iCount_NavigateComplete2
long
> > after the web page is completely loaded. Perhaps I will try Csaba's
> > suggestion to use a readyState of 3. On the website I had problems
with,
> > iCount_BeforeNavigate2 consistently exceeded iCount_NavigateComplete2 by
> > exactly one, so it was easy to make a special case test.
> > ...
> > .Navigate("aol.com")
> > Do While.ReadyState = 4 : WScript.Sleep 2 : Loop
> > Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
> > Do Until iCount_BeforeNavigate2 = iCount_NavigateComplete2
> > WScript.Sleep 100
> > Loop
> > MsgBox "Done navigating to aol.
> > ...
> >
> > My current position is that there is no way for VBScript to know whether
a
> > web page has completely loaded. Hopefully someone will post code to
prove
> > me wrong.
> >
> > -Paul Randall
> >
> >
> >
>
>