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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Driving Me Nuts! How to test for page load in browser control??? 1

Status
Not open for further replies.

MissouriTiger

Programmer
Oct 10, 2000
185
US
I am at my wits end! I know this sounds like an easy question, but I have tried this a hundred ways, and cannot find a way that works and that is practical. Please, somebody help me solve this problem.

My program grabs 3 web pages (one at a time), parses the html to determine if a number on the page has changed, then does it all over again, continuously until a change is detected.

Unfortunatley, I cannot use the Inet control, because the html that the Inet control gets from the server isn't quite the same as what the browser gets, probably due to an idenitty issue (maybe cookie?).

Anyway, I have to use the browser control and get the html from there.

So, I need to do something like this:
................
objBrowser.Navigate someURL

strHTML = objBrowser.Document.body.outerHTML

strResult = GetBetween(strHTML, strStartDelim, strEndDelim)
.................

Seems really simple, right?

Wrong. This code doesn't work, because it executes too quickly, before the browser is finished downloading the HTML.

I realize that I could just end the procedure and then rely on the objBrowser_DocumentComplete() event to execute the rest of the code, but then I would lose the flow of the program. What if the DocumentComplete() event never fires? My program will hang.

So, I tried adding a variety of different loops to wait for the page to download, such as the following:

.........................
objBrowser.Navigate someURL

Do While objBrowser.Busy
Do stuff
Loop

strHTML = objBrowser.Document.body.outerHTML
strResult = GetBetween(strHTML, strStartDelim, strEndDelim)
.........................

But that just causes an infinite loop, and the program has to be forced to quit. As long as the loop is running, the browser won't finish loading the page.

I also tried loops like these:
......................
Do Until (objBrowser.LocationURL = strCurrentURL)
Do stuff
Loop
......................

......................
'This one waits for the DocumentComplete() event to
' set the blnPageLoaded variable to TRUE.

Do Until blnPageLoaded
Do stuff
Loop
......................

NOTHING WORKS!!!

As long as the loop is running, the browser never finishes, the DocumentComplete event never fires, so a loop seems to be out of the question, unless I can somehow start it in a seperate thread, but I've never done multi-threading in VB and I question whether multi-threading is even apropriate here.

Isn't there some way to trap the DocumentComplete event inside my procedure? Or something? I just need for my program to pause momentarilly until the page is loaded.

Please help! By the time I get this program finished my hourly rate will be $2.00 an hour!


Greg Norris
Software Developer

________________________________________________
Constructed from 100% recycled electrons.
 
>Do ...
> Do stuff
>Loop

What kind of stuff do you do in the loop? I just put a DoEvents and it worked fine.

This is what I tested OK.
___
[tt]
Private Sub Form_Load()
Show
WebBrowser1.Navigate [ignore]"www.tek-tips.com"[/ignore]
While WebBrowser1.Busy
DoEvents
Wend
MsgBox "Page ready."
End Sub[/tt]
 
Thanks Hypetia! That is exactly what I was looki8ng for.

Funny thing is, I've seen DoEvents in code samples a couple of times and had assumed it was a pseudocode gibberish word similar to "DoStuff" or "Do Some Junk." I had no idea it was a VB command.

However, for some reason I have not had success with using WebBrowser1.Busy as my test, and had to use (objWB.ReadyState <> 4) instead, which seems to work fine.

I wish I had posted this question sooner, but I like to find ansers on my own and solve my own problems. Perhaps I need to formualte a rule, or set some kind of guideline for myself, like if I don't find a solution to a problem within a given time frame, I ask someone for help. I would really be interested in finding out if anyone else uses some kind of rule or guideline of that sort.

Sometimes I can spend hours wrestling with one small detail like this. When I was in school that was one thing, but now that I'm starting my career, I can't afford to spend hours trying to figure out a small detail.

Anyway, thaks again!

Greg Norris
Software Developer

________________________________________________
Constructed from 100% recycled electrons.
 
My general rule is: if I run out of things to try, or if I'm short of time and don't have time to experiment, I'll ask. I try not to ask unless I need to - I've learned a lot just by experimenting.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Best to set up a flag, and set it.

Code:
'Submit the first (and only) form in the page
WebBrowser1.Document.Forms.Item(0).submit

'Wait for the new page to load again
blnBusy = True
Do While blnBusy = True
    DoEvents
Loop

Code:
Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
blnBusy = True
End Sub

Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
blnBusy = False
End Sub

-David
2006 Microsoft Valueable Professional (MVP)
2006 Dell Certified System Professional (CSP)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top