0

表格 1 代码:

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

    WebBrowser1.Navigate("http://www.----------.org/login.php?")
    WaitForPageLoad()
    WebBrowser1.Document.GetElementById("username").SetAttribute("value", TextBox1.Text)
    WebBrowser1.Document.GetElementById("password").SetAttribute("value", TextBox2.Text)
    WebBrowser1.Document.GetElementById("login").InvokeMember("Click")

End Sub

模块代码:

Private Property pageready As Boolean = False

区域“页面加载功能”

Private Sub WaitForPageLoad()
    AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
    End While
    pageready = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub

结束区域

所以我在互联网上搜索一个可以允许 webbrowser1 在继续下一个代码之前完全加载的模块,我在如何等待 WebBrowser 在 VB.NET 中完全加载?,但我在可访问性方面遇到了一些问题,(在模块 WebBrowser1.* 和主窗体上)它说没有声明,并且由于其保护级别可能无法访问。我是编程新手,希望有人可以帮助我。

这是图片链接 http://s18.postimg.org/8qokcnzh5/Untitled.jpg

4

1 回答 1

1

您的保护级别错误是因为您必须从 Form1 的按钮单击事件中访问WaitForPageLoad()模块WaitForPageLoad中的函数

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    WebBrowser1.Navigate("http://www.----------.org/login.php?")
    WaitForPageLoad.WaitForPageLoad() 'Example: ModuleName.FuncionOrMethodName()
    WebBrowser1.Document.GetElementById("username").SetAttribute("value", TextBox1.Text)
    WebBrowser1.Document.GetElementById("password").SetAttribute("value", TextBox2.Text)
    WebBrowser1.Document.GetElementById("login").InvokeMember("Click")
End Sub
  1. 您需要WaitForPageLoad() 公开您的信息,以便您可以从Form1访问它。
  2. 您将需要使用Form1访问您的WebBrowser1(因为您的 WebBrowser1 位于您的 Form1

新的WaitForPageLoad()

Public Sub WaitForPageLoad()
    AddHandler Form1.WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
    End While
    pageready = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If Form1.WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler Form1.WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub
于 2013-07-14T15:44:45.890 回答