好吧,所以我是新来的,所以如果我发布不正确或有点含糊,我提前道歉。我的问题是NullReferenceException
当我尝试运行我的代码时遇到了一个问题,但是在调试并将鼠标悬停在有问题的变量上时,我确实看到了变量的值。
这是我正在使用的 VB 代码:
Private Sub Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles login.Click
status.Text = "Connecting...."
WebBrowser2.Navigate("http://*****.com/?op=login")
WebBrowser2.Document.GetElementById("loginUsername").InnerText = username.Text
WebBrowser2.Document.GetElementById("loginPassword").InnerText = password.Text
WebBrowser2.Document.GetElementById("loginSubmit").InvokeMember("click")
End Sub
这是正在发生的事情的快照:
------------ 编辑:解决方案--------
WebBrowser2.Url = New Uri("http://*****.com/?op=login")
WaitForPageLoad() ' <---------- ADDED NEW FUNCTION TO WAIT FOR PAGE LOAD
WebBrowser2.Document.GetElementById("loginUsername").InnerText = username.Text
WebBrowser2.Document.GetElementById("loginPassword").InnerText = password.Text
WebBrowser2.Document.GetElementById("loginSubmit").InvokeMember("click")
status.Text = "Completed"
所以我创建了一个名为WaitForPageLoad()的新函数(如何等待 WebBrowser 在 VB.NET 中完全加载中的BGM ?) ,它本质上循环检查页面是否准备就绪,然后一旦准备就绪,就会被杀死处理程序,因此登录成功并且页面不会循环。这是 WaitForPageLoad():
Private Property pageready As Boolean = False
Private Sub WaitForPageLoad()
AddHandler WebBrowser2.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 WebBrowser2.ReadyState = WebBrowserReadyState.Complete Then
pageready = True
RemoveHandler WebBrowser2.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
End If
End Sub