0

I have searched and searched for code and everything I have tried does not work. Basically I need the WebBrowser to fully load before running a test code...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    WebBrowser1.Document.GetElementById("login").SetAttribute("value", TextBox1.Text)
    WebBrowser1.Document.GetElementById("passwd").SetAttribute("value", TextBox2.Text)
    WebBrowser1.Document.GetElementById("SI").InvokeMember("Click")

    Where I need to insert the WaitForPageLoad()


    RichTextBox1.Text = WebBrowser1.DocumentText
    If InStr(RichTextBox1.Text, "To continue, create an Xbox profile") Then
        MsgBox("You do not have an xbox account associated with this gamertag, please log into xbox.com with the account then create an xbox profile.")
    Else
        MsgBox("nothing")

    End If

As you can see I tried to use a script to make me log into Xbox.com, and it does work, but just a little bit. The process of this code is TOO fast, it isn't checking the right source code for the string saying "To continue...", basically

    WebBrowser1.Document.GetElementById("login").SetAttribute("value", TextBox1.Text)
    WebBrowser1.Document.GetElementById("passwd").SetAttribute("value", TextBox2.Text)
    WebBrowser1.Document.GetElementById("SI").InvokeMember("Click")

After it does that click, It clicks the button that does the log in process, but it has to load a whole new page, the problem with this is that it executes the next line of code way too fast and the next line of code searches for that string in the wrong source code. I need it to WAIT for that page to load, then run this line

RichTextBox1.Text = WebBrowser1.DocumentText

Which copies the sourcecode of the webbrowser to a textbox which then is searched for the string. I have tried everything. I feel like WaitForPageLoad() would work great but I get an error telling me it isn't declared. Can anyone help?

John Smith
  • 11
  • 1
  • 1
  • 2
  • 1
    if I understand your question, and im not sure I do, I think you are looking for the webbrowser.DocumentCompleted event. You just want to move your code to that callback event and it will be executed as your document is completely loaded. be aware it can fire multiple times if other documents are loaded by your web page. – John Faulkner Jul 20 '13 at 09:04

3 Answers3

1

You have to add the DocumentCompleted Event Handler and trigger any code from the corresponding method. That is:

Private Sub startBrowser()

    AddHandler WebBrower1.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted

    WebBrower1.Navigate("http://...")

End Sub

Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
       'CALL ALL YOUR CODE FROM HERE
End Sub

---- UPDATE WHOLE WEBBROWSER

If you open a new project and paste this code (and add the TextBoxes/RichTextBox to your form), it would work without any problem:

Public Class Form1
    Friend WithEvents webBrowser0 As New WebBrowser
    Friend WithEvents tabs As New TabControl
    Friend WithEvents tabPage0 As New TabPage

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        startBrowser()
    End Sub
    Public Sub startBrowser()

        Dim url As String = "http://..."

        tabs.Controls.Add(tabPage0)
        tabPage0.Controls.Add(webBrowser0)
        AddHandler webBrowser0.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted

        webBrowser0.Navigate(url)

    End Sub

    Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

        webBrowser0.Document.GetElementById("login").SetAttribute("value", TextBox1.Text)
        webBrowser0.Document.GetElementById("passwd").SetAttribute("value", TextBox2.Text)
        webBrowser0.Document.GetElementById("SI").InvokeMember("Click")



        RichTextBox1.Text = webBrowser0.DocumentText
        If InStr(RichTextBox1.Text, "To continue, create an Xbox profile") Then
            MsgBox("You do not have an xbox account associated with this gamertag, please log into xbox.com with the account then create an xbox profile.")
        Else
            MsgBox("nothing")

        End If

    End Sub
End Class
varocarbas
  • 12,354
  • 4
  • 26
  • 37
  • I get these errors telling me WebBrowser1 isn't declared. It may be inaccessible due to its protection level. That is the same error I got on WaitForPageLoad()... Any help? – John Smith Jul 20 '13 at 09:31
  • @JohnSmith you have to adapt my code to yours. My code assumes that WebBrowser1 exists. I will update my code such that you can use it without relying on yours, although I hope that you are understanding what each bit means (this is the whole point of my answer/this site: helping you to understand where you were wrong, not delivering full working codes). – varocarbas Jul 20 '13 at 09:34
  • I am trying to understand it, but I am confused as to why it would say it's inaccessible due to it's protection level? I have declared WebBrowser1 to the best of my knowledge... – John Smith Jul 20 '13 at 09:36
  • @JohnSmith I have updated my code, just open a new project and paste all this code (add the textBox and richtextbox). It says "inaccessible due to its protection" because the method I created is private and, most likely, your are calling it from an external Class. You can change it to public or move it to the calling class. – varocarbas Jul 20 '13 at 09:41
  • For once somebody is using the correct approach. +1 – Visual Vincent Feb 21 '17 at 19:16
0

This code should help.

Define a global variable called complete and set it to false

Dim completed = false

Now in your web browser document complete put this code in

   Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

   completed = true

   End Sub

Now were you want to wait until your web browser has loaded the page

While Not completed
End While

All together you should have something like this

Public Class WaitForWebBrowser

Dim completed = False

Sub Main()
WebBrowser1.Navigate("http://google.com")

While Not completed
End While
'when the web browser is done complete will be set to true and will exit the while loop and continue your code

End Sub

Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

       completed = true

       End Sub

End Class
washcloth
  • 2,730
  • 18
  • 29
  • **hold on...** Let me suggest a more accurate example of DocumentCompleted from my point of view, see my answer here: http://stackoverflow.com/a/32298026/2396732 – JCM Aug 30 '15 at 15:42
-1
  For I As Integer = 0 To 500
            If MyBrowser.ReadyState = WebBrowserReadyState.Complete Then Exit For
            Threading.Thread.Sleep(1)
            Application.DoEvents()
        Next
JEL
  • 27
  • 2