Web浏览器控件 - 一个接一个地导航到URL

时间:2017-02-17 12:53:41

标签: javascript c# vb.net forms webbrowser-control

我正在开发一个VB.Net类库项目,我有一个Web浏览器控件(在一个表单中),我用它以这个特定的顺序多次导航到同一个URL:

我们假设网址为:https://www.google.com/submitForm

循环开始

  1. 导航到网址
  2. 导航网址的文档完整事件
  3. 执行DOM操作并提交表单
  4. 关闭网络浏览器表单
  5. 循环结束

    代码:

    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
    

    问题: 每次我尝试运行此循环时,它都会打开多个Web浏览器表单,但只关闭第一个表单并保持其他表单打开。此外,它确实执行了多次导航,但实际上只进行了最后一次导航提交。

    预期行为: 我希望表单能够实际完成上述过程,完成每个已完成的事件,关闭表单,然后再次开始创建表单,导航和关闭表单。

1 个答案:

答案 0 :(得分:2)

我自己设法解决了这个问题,并且认为我可以为其他可能尝试解决类似问题的人发布答案。

基本上,我所做的是,而不是逐个循环遍历URL列表,我等待文档完成事件完成,一旦它完成,我使用相同的webbrowserform并执行另一个导航,直到我的所有网址都已完全导航,之后我关闭了表单。

代码:

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