Form1代码:
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在继续下一个代码之前完全加载,我在How to wait until WebBrowser is completely loaded in VB.NET?找到了代码,但我在访问性方面遇到了一些麻烦,(在模块上) WebBrowser1。*和主表单)它表示未声明,并且由于其保护级别而可能无法访问。 我是编程新手,希望有人能帮助我。
答案 0 :(得分: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
WaitForPageLoad()
公开,以便您可以从 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