以下是使用 DocumentCompleted 事件的方法:
Private Sub navBtnClick(sender As Object, e As EventArgs)
Dim wbrowser = New WebBrowser()
wbrowser.DocumentCompleted += New WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted)
wbrowser.Navigate("http://www.somesite.com/login")
End Sub
Public Sub WebBrowser_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
Dim webBrowser = TryCast(sender, WebBrowser)
webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted
'MessageBox.Show(webBrowser.Url.ToString())
webBrowser.Document.GetElementById("login").SetAttribute("value", txtUsername.Text)
webBrowser.Document.GetElementById("passwd").SetAttribute("value", txtPassword.Text)
webBrowser.Document.GetElementById("SI").InvokeMember("Click")
End Sub
如果要导航到的文档具有嵌入其他网页的 iframe,则 DocumentCompleted 事件会触发多次。如果您只想接收一次事件,只需取消订阅 DocumentCompleted 处理程序,如上所示。