2013-07-20 47 views
0

我已經搜索並搜索了代碼,我嘗試過的所有東西都不起作用。 基本上我所需要的web瀏覽器運行測試代碼之前完全加載...等到WebBrowser加載? (VB 2010)

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

    WebBrowser1.Document.GetElementById("login").SetAttribute("value", TextBox1.Text) 
    WebBrowser1.Document.GetElementById("passwd").SetAttribute("value", TextBox2.Text) 
    WebBrowser1.Document.GetElementById("SI").InvokeMember("Click") 

    Where I need to insert the WaitForPageLoad() 


    RichTextBox1.Text = WebBrowser1.DocumentText 
    If InStr(RichTextBox1.Text, "To continue, create an Xbox profile") Then 
     MsgBox("You do not have an xbox account associated with this gamertag, please log into xbox.com with the account then create an xbox profile.") 
    Else 
     MsgBox("nothing") 

    End If 

正如你可以看到我試圖用一個腳本來讓我登錄到Xbox.com,和它的工作,但只是一點點。這段代碼的過程太快,不檢查字符串說:「要繼續......」權源代碼,基本上

WebBrowser1.Document.GetElementById("login").SetAttribute("value", TextBox1.Text) 
    WebBrowser1.Document.GetElementById("passwd").SetAttribute("value", TextBox2.Text) 
    WebBrowser1.Document.GetElementById("SI").InvokeMember("Click") 

它確實是點擊,發出滴答聲,做的按鈕後登錄過程中,但它必須加載一個全新的頁面,問題在於它執行下一行代碼的速度太快,下一行代碼在錯誤的源代碼中搜索該字符串。我需要它來等待頁面加載,然後運行該行

RichTextBox1.Text = WebBrowser1.DocumentText 

哪個拷貝網頁瀏覽器的源代碼,其隨後被搜索的字符串文本框。我已經嘗試了一切。我覺得WaitForPageLoad()會很好,但是我得到一個錯誤,告訴我它沒有被聲明。誰能幫忙?

+1

如果我明白你的問題,和林不知道我這樣做,我認爲你正在尋找webbrowser.DocumentCompleted事件。你只是想把你的代碼移動到回調事件中,並且它會在你的文檔被完全加載時執行。請注意,如果其他文檔由您的網頁加載,它可以多次啓動。 –

回答

0

此代碼應該有所幫助。

定義一個名爲完整的全局變量,並將其設置爲false

Dim completed = false 

現在,在Web瀏覽器中的文件完全把這個代碼在

Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) 

    completed = true 

    End Sub 

現在是你要等待,直到你的網頁瀏覽器已加載頁面

While Not completed 
End While 

所有在一起,你應該有類似的東西這

Public Class WaitForWebBrowser 

Dim completed = False 

Sub Main() 
WebBrowser1.Navigate("http://google.com") 

While Not completed 
End While 
'when the web browser is done complete will be set to true and will exit the while loop and continue your code 

End Sub 

Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) 

     completed = true 

     End Sub 

End Class 
+0

**堅持... **讓我從我的角度提出一個更精確的DocumentCompleted示例,請參閱我的答案:http://stackoverflow.com/a/32298026/2396732 – JCM

1

您必須添加DocumentCompleted Event Handler並觸發相應方法的任何代碼。那就是:

Private Sub startBrowser() 

    AddHandler WebBrower1.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted 

    WebBrower1.Navigate("http://...") 

End Sub 

Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) 
     'CALL ALL YOUR CODE FROM HERE 
End Sub 

---- UPDATE全WEBBROWSER

如果你打開一個新的項目並粘貼此代碼(並添加TextBoxes/RichTextBox到您的形式),它的工作沒有任何問題:

Public Class Form1 
    Friend WithEvents webBrowser0 As New WebBrowser 
    Friend WithEvents tabs As New TabControl 
    Friend WithEvents tabPage0 As New TabPage 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     startBrowser() 
    End Sub 
    Public Sub startBrowser() 

     Dim url As String = "http://..." 

     tabs.Controls.Add(tabPage0) 
     tabPage0.Controls.Add(webBrowser0) 
     AddHandler webBrowser0.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted 

     webBrowser0.Navigate(url) 

    End Sub 

    Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) 

     webBrowser0.Document.GetElementById("login").SetAttribute("value", TextBox1.Text) 
     webBrowser0.Document.GetElementById("passwd").SetAttribute("value", TextBox2.Text) 
     webBrowser0.Document.GetElementById("SI").InvokeMember("Click") 



     RichTextBox1.Text = webBrowser0.DocumentText 
     If InStr(RichTextBox1.Text, "To continue, create an Xbox profile") Then 
      MsgBox("You do not have an xbox account associated with this gamertag, please log into xbox.com with the account then create an xbox profile.") 
     Else 
      MsgBox("nothing") 

     End If 

    End Sub 
End Class 
+0

我得到這些錯誤告訴我WebBrowser1沒有聲明。由於其保護級別,它可能無法訪問。這是我在WaitForPageLoad()上得到的同樣的錯誤...任何幫助? –

+0

@JohnSmith你必須讓我的代碼適應你的。我的代碼假定WebBrowser1存在。我會更新我的代碼,以便您可以在不依賴於您的情況下使用它,儘管我希望您瞭解每個位的含義(這是我答案/本網站的重點:幫助您瞭解您錯誤的位置,而不是提供完整的工作代碼)。 – varocarbas

+0

我試圖理解它,但我很困惑,爲什麼它會說由於它的保護級別而無法訪問?我已經宣佈WebBrowser1到我所知道的最好... –

-1
For I As Integer = 0 To 500 
      If MyBrowser.ReadyState = WebBrowserReadyState.Complete Then Exit For 
      Threading.Thread.Sleep(1) 
      Application.DoEvents() 
     Next