2013-07-20 51 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