Neophile
Neophile

Reputation: 5870

Web Browser Control - Navigate to URL one after the other

I am working on a VB.Net class library project and I have a web browser control (within a form) that I am using to navigate to the same URL multiple times in this particular sequence:

Let's assume the URL is: https://www.google.com/submitForm

Loop Start

  1. Navigate to URL
  2. Document Complete Event of the navigated URL
  3. Perform DOM manipulation and submit form
  4. Close web browser form

Loop End

Code:

Public Sub customNavigation()
    For j = 0 To listOfUrls.Count - 1
                    testWebBrowserForm = New WebBrowserForm(Me)
                    Dim browserSize As System.Drawing.Size = New Size(100, 100)
                    testWebBrowserForm.Size = browserSize
                    testWebBrowserForm.FormBorderStyle = FormBorderStyle.FixedSingle
                    testWebBrowserForm.Show()
                    testWebBrowserForm.SendToBack()
                    testWebBrowserForm.Location = New Point(100, 100)

                    testWebBrowserForm.Navigate(New Uri("https://google.com/submitForm"))
                Next
End Sub

// Once the document has completely loaded
Public Sub documentLoadComplete()

        Dim submitButton As HtmlElement = Nothing, formEl As HtmlElement = Nothing

        Dim attachmentInputElements As Windows.Forms.HtmlElementCollection = testWebBrowserForm.webBrowser.Document.GetElementsByTagName("input")
        Dim formElements As Windows.Forms.HtmlElementCollection = testWebBrowserForm.webBrowser.Document.Forms
        Dim form As Windows.Forms.HtmlElement = testWebBrowserForm.webBrowser.Document.Forms(0)

        For y = 0 To formElements.Count - 1
            Dim formelement As HtmlElement = formElements(y)
            If formelement.GetAttribute("name").Equals("theForm") Then
                formEl = formelement
            End If
        Next

        For i = 0 To attachmentInputElements.Count - 1
            Dim inputElement As HtmlElement = attachmentInputElements(i)
            If inputElement.GetAttribute("type").Equals("submit") Then
                submitButton = inputElement
            End If
        Next

        testWebBrowserForm.webBrowser.Document.InvokeScript("doSomething")

        submitButton.InvokeMember("click")

        testWebBrowserForm.Close()
End Sub

Issue: Every time I try to run this loop, it does open multiple web browser forms but only closes the first one and keeps the other ones open. Plus, it does perform the navigate multiple times but only actually does the last navigation submission.

Expected Behaviour: I would like for the form to actually go through the process mentioned above, go through each completed event, close the form and then start the creation of the form, navigation and close form again.

Upvotes: 0

Views: 1056

Answers (1)

Neophile
Neophile

Reputation: 5870

I managed to solve this issue myself and thought I'd post the answer for someone else who might be trying to resolve a similar issue.

Basically, what I did was, instead of looping through the list of URL's one by one, I waited for the documentcomplete event to finish and once it does that, I use the same webbrowserform and perform another navigate until all my URL's have been completely navigated, after which I close the form.

Code:

Public currentUrlIndex As Integer = Nothing, currentUrl As String = Nothing
    Public Sub customNavigation()

                        testWebBrowserForm = New WebBrowserForm(Me)
                        Dim browserSize As System.Drawing.Size = New Size(100, 100)
                        testWebBrowserForm.Size = browserSize
                        testWebBrowserForm.FormBorderStyle = FormBorderStyle.FixedSingle
                        testWebBrowserForm.Show()
                        testWebBrowserForm.SendToBack()
                        testWebBrowserForm.Location = New Point(100, 100)
                        currentUrlIndex = 0
                        currentUrl = listOfUrls(currentUrlIndex)
                        testWebBrowserForm.Navigate(New Uri(currentUrl))

    End Sub

    // Once the document has completely loaded
    Public Sub documentLoadComplete()

            Dim submitButton As HtmlElement = Nothing, formEl As HtmlElement = Nothing

            Dim attachmentInputElements As Windows.Forms.HtmlElementCollection = testWebBrowserForm.webBrowser.Document.GetElementsByTagName("input")
            Dim formElements As Windows.Forms.HtmlElementCollection = testWebBrowserForm.webBrowser.Document.Forms
            Dim form As Windows.Forms.HtmlElement = testWebBrowserForm.webBrowser.Document.Forms(0)

            For y = 0 To formElements.Count - 1
                Dim formelement As HtmlElement = formElements(y)
                If formelement.GetAttribute("name").Equals("theForm") Then
                    formEl = formelement
                End If
            Next

            For i = 0 To attachmentInputElements.Count - 1
                Dim inputElement As HtmlElement = attachmentInputElements(i)
                If inputElement.GetAttribute("type").Equals("submit") Then
                    submitButton = inputElement
                End If
            Next

            testWebBrowserForm.webBrowser.Document.InvokeScript("doSomething")

            submitButton.InvokeMember("click")

            If currentUrlIndex = listOfUrls.Count - 1 Then
                    testWebBrowserForm.Close()
                    Exit Sub
                Else
                    currentUrlIndex = currentUrlIndex + 1
                    currentUrl = listOfUrls(currentUrlIndex)
                    testWebBrowserForm.Navigate(New Uri(currentUrl))
                End If
    End Sub

Upvotes: 2

Related Questions