Browser waiting

I'm trying to put together a relatively simple script for a webbrowser game that provides a service in-game by interacting with the site and doing some stuff with the material within it. I have gotten around most of the problems of how to make it work, but there's one thing that I don't know how to do properly: making the script wait for the browser.

In short: I'd need some way of making the script check that the browser has finished downloading the page before attempting to do anything else. Here's a snippet of what I currently have:

Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _

Handles MyBase.Load

WebBrowser1.Navigate("http://www.WEBSITE.com")

End Sub

Public Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, _

ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _

Handles WebBrowser1.DocumentCompleted

If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then

Login()

End If

End Sub

Public Sub Login()

WebBrowser1.Document.GetElementById("loginname").InnerText = "LOGINNAME"

WebBrowser1.Document.All.GetElementsByName("password")(0).InnerText = "PASSWORD"

WebBrowser1.Document.Forms(0).InvokeMember("submit")

If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then

Messages()

End If

End Sub

It's supposed to open the frontpage, fill the login fields and then submit the stuff. (Which works well enough) But after when it has logged in, when it's supposed to navigate to the inbox, things crash and burn. The problem seems to be that WebBrowser1_DocumentCompleted, it executes EACH TIME the event is raised, which doesn't really work as I need the script to access more than just one page in succession. You can see the attempt I made with WebBrowser.ReadyState, but that does not work either.

I did search around the forum, but the best example I found was a single page navigation that used the same event as I'm trying to. So, any working solutions to this would be greatly appreciated.

I could of course implement a simple timer that waits a couple of seconds after each navigation attempt, but I would prefer to avoid that because 1) it would waste some time waiting when the page is already loaded and 2) when the site is lagged, the delay might not be long enough and so the whole concept fails. I am using VB 2005 Express and I am also quite a beginner (as you have likely noticed).



Answer this question

Browser waiting

  • YFell

    nobugz wrote:
    Yes, that's not pretty. All you need to do is to keep track of what you're doing and respond accordingly in the DocumentCompleted event. For example: <snip>


    Thanks, that's much more refined than the way I came up with.


    As for asalcedo: I think that's bit of an overkill, because even though the site uses frames as a part of the interface, I'm only doing stuff in one of them, like this:

    WebBrowser1.Navigate(http://www.website.com, "mainpane")

    Regardless, I also thank you for chiming in and helping out. If I do ever need to deal with a site that uses frames extensively, I'll consult your example.

  • gokhanmutlu

    Try

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=801353&SiteID=1

    Although it requires to find out tfc in advance it is the only method I have found that works reliably.

    It is however, an issue that I have not seen solved properly yet. After much work on it.


  • QWERTY890

    Yes, that's not pretty. All you need to do is to keep track of what you're doing and respond accordingly in the DocumentCompleted event. For example:

    Private Enum WebdocState
    Idle
    Navigating
    Authenticating
    ' Add more as needed
    End Enum
    Private mState As WebdocState = WebdocState.Idle

    Set mState to Navigating when you call the Navigate method, set it to Authenticating when you submit the form. And use the mState value in DocumentCompleted to know what to do next.


  • Vaish

    Well, I did manage to find a way to do it, but it's not elegant. Here's the code:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    WebBrowser1.Navigate("http://www.WEBSITE.com")
    End Sub
    Public Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
    Handles WebBrowser1.DocumentCompleted
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
    Login()
    End If
    End Sub
    Public Sub Login()
    Do Until (WebBrowser1.ReadyState = WebBrowserReadyState.Complete)
    InitializeTimer()
    Loop
    WebBrowser1.Document.GetElementById("loginname").InnerText = "LOGINNAME"
    WebBrowser1.Document.All.GetElementsByName("password")(0).InnerText = "PASSWORD"
    WebBrowser1.Document.Forms(0).InvokeMember("submit")
    End Sub
    Private Sub InitializeTimer()
    Timer1.Interval = 250
    Timer1.Start()
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Timer1.Stop()
    End Sub

    Any improvements or alternatives are still welcome.

  • Browser waiting