0

我已经搜索并搜索了代码,但我尝试过的一切都不起作用。基本上我需要在运行测试代码之前完全加载 WebBrowser...

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,它确实有效,但只是一点点。这段代码的过程太快了,它基本上没有检查字符串“To continue...”的正确源代码

    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() 会很好用,但我收到一条错误消息,告诉我它没有被声明。任何人都可以帮忙吗?

4

3 回答 3

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

---- 更新整个网络浏览器

如果您打开一个新项目并粘贴此代码(并将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
于 2013-07-20T09:07:09.793 回答
0

这段代码应该会有所帮助。

定义一个名为 complete 的全局变量并将其设置为 false

Dim completed = false

现在在您的网络浏览器文档中完成将此代码放入

   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
于 2013-07-20T18:34:31.517 回答
-1
  For I As Integer = 0 To 500
            If MyBrowser.ReadyState = WebBrowserReadyState.Complete Then Exit For
            Threading.Thread.Sleep(1)
            Application.DoEvents()
        Next
于 2014-03-18T20:41:44.617 回答