0

好吧,我有一个脚本可以通过以下方式登录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,但如果有人能引导我走上正确的道路,我将不胜感激。

4

1 回答 1

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
于 2013-03-24T11:20:19.863 回答