[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2064
  • Last Modified:

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?


0
Matrix1000
Asked:
Matrix1000
  • 3
  • 3
  • 2
  • +2
3 Solutions
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now