【问题标题】:Display error message when wrong password entered输入错误密码时显示错误消息
【发布时间】:2013-03-13 12:44:51
【问题描述】:

好的,我有一个脚本可以通过以下方式登录live.com

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

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

【问题讨论】:

    标签: html vb.net login credentials account


    【解决方案1】:

    如果您想通过网页浏览器中返回的内容进行评估,您可以搜索因输入密码而加载的文档的 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
    

    【讨论】:

    • 哇,约翰!这对我的方式非常有效。然而,我唯一感到沮丧的是,每当程序加载时,它往往会说“错误的帐户凭据!”每次程序打开时(所以我必须单击确定)然后我可以测试程序并且它运行良好(告诉我是否使用了错误的凭据或正确的凭据)。每次加载页面时它也会提示我 3 次,所以我也很困惑。为什么会这样? pastebin.com/K8Mrq8FR
    • 在做出决定后尝试添加“htmlText = Nothing”(例如,清除变量的内容)。您可以发布更多代码以供审核吗?
    • 嗯...试过了,但没有用。这是完整的代码:pastebin.com/MmiTJ99j 我不知道它必须处理什么,这很令人困惑,为什么它会提示我输入错误的凭据 3 次,但是当我输入正确的代码时,它只会发送消息我曾经。
    • 问题出在“.DocumentCompleted”事件处理程序上;它会在框架完成时触发,不一定是整个文档。建议的方法是使用上面的“ReadyState”方法。这是问题的link。我只需将您的“.DocumentCompleted”事件处理程序更改为在您导航到网页后调用的函数。
    • 是的,我明白,但你引用我的链接是 C#,我没有任何经验。
    猜你喜欢
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    相关资源
    最近更新 更多