2017-02-17 5 views
0

私はVB.Netクラスライブラリプロジェクトで作業しています。同じURLに複数回移動するためのWebブラウザコントロール(フォーム内)があります。この特定のシーケンス:Webブラウザコントロール - 次々とURLに移動します。

のは、URLを想定してみましょうです:https://www.google.com/submitForm

ループスタート

  1. 移動
  2. ドキュメント完了イベントをURLにナビゲートURLの
  3. はフォームDOM操作を実行し、提出
  4. 閉じるWebブラウザーフォーム

ループエンド

コード:

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

こんにちは@VisualVincent:フォームの関連コードも追加しました。これを達成する方法があれば教えてください。 – Neophile

+0

フォームの実行が最後のものであると仮定します.... me.closeに従います –

答えて

2

私はこの問題を自分で解決することができ、同様の問題を解決しようとしている他の人に回答を投稿すると思いました。

基本的には、URLのリストを1つずつループするのではなく、documentcompleteイベントが完了するのを待っていました。同じWebbrowserformを使用して、すべてのURL完全にナビゲートされた後、私はフォームを閉じます。

コード:

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 
関連する問題