WebBrowser - How to Wait till page is loaded?

I have a form with a webbrowser control on it.
What I'm trying to do is on Form_Load load a page in the webbrowser. THEN once the form is loaded and the first page is fully loaded, fill in the login/password fields and auto click "Submit":

With AxWebBrowser1
.navigate2 www.thefirstpage.com  'THEN ONCE THE PAGE IS COMPLETELY LOADED...
,
        .Document.Forms("login").username.value = "myLogin"
        .Document.Forms("login").password.value = "myPassword"
        .Document.Forms("login").submit()

'THEN ONCE THE *NEXT* PAGE IS COMPLETELY LOADED...

.navigate2 www.thesecondpage.com etc.

End With

I cant seem to figure out how to wait until the page is loaded fully before filling it in or navigating to the second page.

My goal is to open the application > automatically go to the webpage > automatically fill in the username/password and submit it to login > wait for the login to complete > go to another form so the user can complete it.

How could I do that?


LVL 1
Matrix1000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeff CertainCommented:
When you call the original webpage, send the user name and password as part of the querystring. Then, in your Page_Load, check for the existence of the user name and password in the querystring and submit the login if they exist.

PageLoad happens after the page is pretty much loaded anyhow.
0
Bob LearnedCommented:
The WebBrowser has a DocumentComplete event:

  Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete

  End Sub

Bob
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
As Bob said...something like this may work for you:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With AxWebBrowser1
            .Navigate2("www.thefirstpage.com")
        End With
    End Sub

    Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
        Static firstPage As Boolean = True
        If firstPage Then
            firstPage = False
            With AxWebBrowser1
                .Document.Forms("login").username.value = "myLogin"
                .Document.Forms("login").password.value = "myPassword"
                .Document.Forms("login").submit()
            End With
        Else
            AxWebBrowser1.Navigate2("www.thesecondpage.com")
        End If
    End Sub

End Class
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Bob LearnedCommented:
Mike, I couldn't have said it better myself :)  Get outta my mind, man * Grin *

Bob
0
acharbonneauCommented:
Add an event handler and wire it up to the DocumentComplete event of the webbrowser object.

So, in your form's constructor, add this line:

axWebBrowser1.DocumentComplete += new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(axWebBrowser1_DocumentComplete);

Then define your handler procedure as follows:

//this will be fired when page is completely loaded
private void axWebBrowser1_DocumentComplete2(object sender,
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
      {                  
      //code that needs to run after page is completely loaded goes HERE
      }
0
Bob LearnedCommented:
Is there an echo in here? * Grin *
0
Matrix1000Author Commented:
Wouldn't  "Private Sub AxWebBrowser1_DocumentComplete()" perform the function after EVERY page load then and keep taking the user back to the www.thesecondpage.com?
Just curious because the form I need is inside a framed page so I cant tell it by its url :/

I've tried the following but it doesn't want to load the second page and thus the form because the process is in form load and it cant load the second page until formload finishes :P

-----------CODE-------------------
   Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AxWebBrowser1.Navigate2("http:\\www.MYSITE.com")

        With AxWebBrowser1
            Do While .Busy = True
                Application.DoEvents()
            Loop

            Try
                .Document.Forms("login").username.value = "MYLOGIN"
                .Document.Forms("login").password.value = "MYPASSWORD"
                .Document.Forms("login").submit()
            Catch
                Console.WriteLine("An error occurred.")
            End Try

            Do While .ReadyState <> SHDocVw.tagREADYSTATE.READYSTATE_LOADED '<----CANT GET PAST HERE :(
                Application.DoEvents()
            Loop
            .Navigate2("https://www.MYSITE.com/applications/loan_app/appv2_start.asp?start=Y")
        End With
    End Sub
0
Matrix1000Author Commented:
Idle_Mind - THANKS!  BUT
it is making a loop...reloading   AxWebBrowser1.Navigate2("www.thesecondpage.com") over and over :)
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok...so use a counter instead of a boolean:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AxWebBrowser1.Navigate2("http:\\www.MYSITE.com")
    End Sub

    Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
        Static page As Integer = 1
        Select Case page
            Case 1
                With AxWebBrowser1
                    .Document.Forms("login").username.value = "myLogin"
                    .Document.Forms("login").password.value = "myPassword"
                    .Document.Forms("login").submit()
                End With
                page = page + 1

            Case 2
                AxWebBrowser1.Navigate2("www.thesecondpage.com")
                page = page + 1

        End Select
    End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Matrix1000Author Commented:
K THANKS!...I tried the following and it seemed to work too :)

    Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
        Static FirstPage As Boolean = True
        Static SecondPage As Boolean = True
        If FirstPage Then
            FirstPage = False
            With AxWebBrowser1
                .Document.Forms("login").username.value = "MYLOGIN"
                .Document.Forms("login").password.value = "MYPASS"
                .Document.Forms("login").submit()
            End With
        ElseIf SecondPage Then
            SecondPage = False
            AxWebBrowser1.Navigate2("https://www.MYSITE.com/applications/loan_app/appv2_start.asp?start=Y")
        Else
            Exit Sub
        End If

    End Sub
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.