如果您想通过 webbrowser 中返回的内容进行评估,您可以搜索因输入密码而加载的文档的 innerhtml:
With WebBrowser1
Do Until Not (.IsBusy)
Application.DoEvents()
Loop
Do Until .ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
Loop
End With
Dim htmlText As String
If Not (WebBrowser1.Document.Body.InnerHtml) Is Nothing Then
htmlText = WebBrowser1.Document.Body.InnerHtml
If InStr(htmlText, "That Microsoft account doesn't exist.") Then
Messagebox.Show("Wrong Account Credentials")
'code to go here if it is true
Else
Messagebox.Show("Correct Account Credentials")
'code to go here if it is false
End If
End If
注意:如果 innerhtml 不起作用,您也可以尝试 outerhtml。
由于您只想验证一次,请尝试删除 .DocumentChanged 事件处理程序,因为除了初始登录之外,它的触发原因不同;将其替换为在登录时调用“Click”事件后调用的子例程。
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
WebBrowser1.Document.GetElementById("login").SetAttribute("value", txtUsername.Text)
WebBrowser1.Document.GetElementById("passwd").SetAttribute("value", txtPassword.Text)
WebBrowser1.Document.GetElementById("SI").InvokeMember("Click")
verifyLogin()
End Sub
Private Sub verifyLogin()
With WebBrowser1
Do Until Not (.IsBusy)
Application.DoEvents()
Loop
Do Until .ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
Loop
End With
Dim htmlText As String
If Not (WebBrowser1.Document.Body.InnerHtml) Is Nothing Then
htmlText = WebBrowser1.Document.Body.InnerHtml
If InStr(htmlText, "Microsoft account") Then
MessageBox.Show("You have entered in a wrong password or the account doesn't exist.")
'code to go here if it is true
Else
MessageBox.Show("Sign in successful. Proceed on...")
'code to go here if it is false
End If
End If
End Sub