输入错误密码时显示错误消息

时间:2013-03-23 19:18:15

标签: html vb.net login credentials account

好吧,我有一个登录live.com的脚本,通过这个:

     WebBrowser1.Document.GetElementById("login").SetAttribute("value", txtUsername.Text)
     WebBrowser1.Document.GetElementById("passwd").SetAttribute("value", txtPassword.Text)
     WebBrowser1.Document.GetElementById("SI").InvokeMember("Click")

这是非常基本的,但现在我需要知道用户是否输入了正确或错误的帐户凭据。因此,这将需要一条错误消息“无法登录”,但如果成功,我只需将webbrowser重定向到新页面。现在我不熟悉VB上的IF语句,但我知道有两种方法可以做到,但不知道怎么做。因此,第一种方法是在点击提交按钮后读取它所访问的URL,这将非常好并且有效(因此当有人输入不正确的帐户凭据时,它会将它们发送到错误页面,但是当您输入正确的时候它会把你送到正确的页面)。但我想要做的主要方式是阅读内容。如果页面在提交后显示“That Microsoft account doesn't exist. Enter a different email address or get”,则会显示消息框“Wrong Account Credentials”,反之亦然。我对此并不擅长,因为我从未真正使用过WebBrowser,但如果有人能以正确的方式引导我,我会非常感激。

1 个答案:

答案 0 :(得分:0)

如果您想根据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
相关问题