WebBrowser - waiting for page to load

B

BobRoyAce

I am attempting to use a WebBrowser on a Windows Form application, in
a situation where I am wanting to automate the following:

1) Click on all checkboxes on page
2) Click on link to go to new page

I have successfully coded the above two steps.

Now, what I want to do is do the above two steps a repeated number of
times. So, I try to do it in a loop. The problem is, step 1 attempts
to run before step 2 has completed (i.e. the new page has loaded).

How can I change code so that step 1 won't run until step 2 has
completed?

Private Sub GetEmAll()
For i As Integer = 1 To 10
GetContactsOnCurrentPage()

'*** WHAT SHOULD I DO HERE?
Next
End Sub

Private Sub GetContactsOnCurrentPage()
' Check all checkboxes on page
Dim oCheckBoxes As HtmlElementCollection = GetElements()

For Each oElement As HtmlElement In oCheckBoxes
If (oElement.OuterHtml.Contains("dgSelect_ct")) Then
oElement.InvokeMember("Click")
End If
Next

' Click on link to go to next page
Dim oNext As HtmlElement =
WebBrowser1.Document.GetElementById("btnNextPage")
oNext.InvokeMember("Click")
End Sub
 
N

Norman Yuan

Check out WebBrowser control's DocumentCompleted event. That is, you can
start "step 2" in the DocumentCompleted event handler, when step 1 loads its
document.
 
B

BobRoyAce

Check out WebBrowser control's DocumentCompleted event. That is, you can
start "step 2" in the DocumentCompleted event handler, when step 1 loads its
document.
I'll check that out. This makes me wonder, though, how I would handle
things if there were a bunch of times that I needed to wait for pages
to load. For example, let's say that I wanted to automate going
through a series of pages like follows:

Go to Page 1
On Page 1, enter login information and click on Login button.
On Page 2, click on an option and click on Next button.
On Page 3, enter values in textboxes and click on Next button.
On Page 4, start looping process I described in original post.

Now, the DocumentCompleted control would have to handle a whole bunch
of different scenarios. In other words, the code that it would need to
execute would vary, depending on what page we were on.
 
N

Norman Yuan

You can have a bunch of module/form level boolean variables to set a loaded
flag for each step/page with initial value as False. Or have one single
varaible to hold current step/page number. This/these variable is set with
each step/page loaded. So, you should alway know each step/page the user is
currently with, thus, also know what the nest step/page should be.
 
B

BobRoyAce

You can have a bunch of module/form level boolean variables to set a loaded
flag for each step/page with initial value as False. Or have one single
varaible to hold current step/page number. This/these variable is set with
each step/page loaded. So, you should alway know each step/page the user is
currently with, thus, also know what the nest step/page should be.
Yea, that works well for page to page stuff, and I implemented it for
all but the "looped" functionality that mentioned. Unfortunately, I
couldn't use it for that as it turned out that that page was not
reloading each time the next page link was being clicked on.
Apparently, it was a .NET grid using Ajax or something, and, so, after
clicking on the next page button, it just dynamically updated what was
shown in the table without reloading the page. As a result, the
DocumentCompleted event was not firing. So, what I ended up doing for
this was putting a Timer control on the form and having it fire every
four seconds. Each time it fires, it executes the code to check all
the checkboxes and click on the link to go to the next page. It works
great. Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top