Hello people :) I need some help. How can I get Webbrowser1 to navigate to each page and wait for the one page to completely load, then move to the next page...?

Thank you for answers in advance! ;)


Example.

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



WebBrowser1.Navigate("http://www.google.com") 'Wait for a page to completely load and continue...


WebBrowser1.Navigate("http://www.daniweb.com/forums/")   'Wait for a page to completely load and continue...


WebBrowser1.Navigate("http://msdn.microsoft.com/lt-lt/default(en-us).aspx")     'Wait for a page to completely load and continue...



WebBrowser1.Navigate("")     'Wait for a page to completely load and continue...

End Sub

I tried this, but it doesnt work well.

If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
WebBrowser1.Navigate("")
        End If

Dani AI

Generated

Timers can work for demos (nice example by @Unhnd_Exception), but for reliable sequencing you want to drive the flow from the WebBrowser events. Note that DocumentCompleted can fire multiple times on pages with frames; only proceed when the event corresponds to the top-level document. A common pattern is to check that e.Url equals WebBrowser1.Url and that the control is not busy before moving on. See the WebBrowser event docs and the older IE control notes that explain the multiple-fire behavior. WebBrowser.DocumentCompleted and DocumentComplete event details.

Here is an event-driven queue that navigates, waits for the page to finish, does work, then moves to the next URL. No sleeps or DoEvents needed.

' Form-level field
Private _queue As Queue(Of String)

Public Sub StartNav(urls As IEnumerable(Of String))
    _queue = New Queue(Of String)(urls)
    NavigateNext()
End Sub

Private Sub NavigateNext()
    If _queue Is Nothing OrElse _queue.Count = 0 Then Exit Sub
    WebBrowser1.Navigate(_queue.Dequeue())
End Sub

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) _
    Handles WebBrowser1.DocumentCompleted

    Dim wb = CType(sender, WebBrowser)
    If e.Url <> wb.Url OrElse wb.IsBusy OrElse wb.ReadyState <> WebBrowserReadyState.Complete Then Return

    ' Example: collect links from the loaded page
    If wb.Document IsNot Nothing Then
        For Each a As HtmlElement In wb.Document.GetElementsByTagName("a")
            Dim href = a.GetAttribute("href")
            If Not String.IsNullOrEmpty(href) Then ListBox1.Items.Add(href)
        Next
    End If

    NavigateNext()
End Sub

For : if your goal is to harvest Google results across pages, be aware scraping search results is restricted. Consider the official Programmable Search JSON API instead; it returns results and provides a nextPage token for paging. Custom Search JSON API introduction. Also, only access the DOM after DocumentCompleted to avoid null references. Access the managed HTML DOM.

Recommended Answers

All 3 Replies

Member Avatar for Member #857553

Heres "A" way to do it.

This will navigate back and forth from google.com and yahoo.com, every 10 seconds after the page is loaded.

Public Class Form1
    Private urls() As String = {"http://www.google.com", _
                                "http://www.yahoo.com"}
    Private index As Integer
    Private WithEvents navtimer As New Timer

    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        navtimer.Interval = 10000
        WebBrowser1.Navigate(urls(index))
        index += 1
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        lblDone.Text = urls(index - 1)
        navtimer.Start()
    End Sub

    Private Sub navtimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles navtimer.Tick
        If WebBrowser1.IsBusy Then Exit Sub
        If index > UBound(urls) Then index = 0
        WebBrowser1.Navigate(urls(index))
        index += 1

        navtimer.Stop()
    End Sub
End Class
commented: Thanks for help! :) +0

Thanks man it works great :) +rep

I have a simple question.
I'm making google urls bot and your code it works great,but it
is not in this way i want to.
It loads well but in form load.

Can someone knows how to load and move next page and so on itself and collect all links from google using textbox or listbox in document_complated ?

To make it loop for all google pages like 1,2,3,4,5,6,7,8,9,10 and so on.

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.