vb.net 等到 WebBrowser 加载?(VB 2010)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17760449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:19:33  来源:igfitidea点击:

Wait Till WebBrowser Load? (VB 2010)

vb.netformsloadwait

提问by John Smith

I have searched and searched for code and everything I have tried does not work. Basically I need the WebBrowser to fully load before running a test code...

我已经搜索并搜索了代码,但我尝试过的一切都不起作用。基本上我需要 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

As you can see I tried to use a script to make me log into Xbox.com, and it does work, but just a little bit. The process of this code is TOO fast, it isn't checking the right source code for the string saying "To continue...", basically

如您所见,我尝试使用脚本让我登录 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")

After it does that click, It clicks the button that does the log in process, but it has to load a whole new page, the problem with this is that it executes the next line of code way too fast and the next line of code searches for that string in the wrong source code. I need it to WAIT for that page to load, then run this line

点击之后,它点击了执行登录过程的按钮,但它必须加载一个全新的页面,问题在于它执行下一行代码的速度太快,下一行代码搜索对于错误源代码中的那个字符串。我需要它等待该页面加载,然后运行这一行

RichTextBox1.Text = WebBrowser1.DocumentText

Which copies the sourcecode of the webbrowser to a textbox which then is searched for the string. I have tried everything. I feel like WaitForPageLoad() would work great but I get an error telling me it isn't declared. Can anyone help?

它将网络浏览器的源代码复制到一个文本框,然后搜索该字符串。我已经尝试了一切。我觉得 WaitForPageLoad() 会很好用,但我收到一个错误,告诉我它没有被声明。任何人都可以帮忙吗?

回答by varocarbas

You have to add the DocumentCompleted Event Handlerand trigger any code from the corresponding method. That is:

您必须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 WHOLE WEBBROWSER

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

If you open a new project and paste this code (and add the TextBoxes/RichTextBoxto your form), it would work without any problem:

如果您打开一个新项目并粘贴此代码(并将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

回答by Washcloth

This code should help.

此代码应该有所帮助。

Define a global variable called complete and set it to false

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

Dim completed = false

Now in your web browser document complete put this code in

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

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

   completed = true

   End Sub

Now were you want to wait until your web browser has loaded the page

现在是否要等到 Web 浏览器加载页面

While Not completed
End While

All together you should have something like this

总之,你应该有这样的东西

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

回答by JEL

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