2013-03-23 112 views
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合作过,但如果任何人都能以正确的方式引导我,我会非常感激。

回答

0

如果你想通过什么在网页浏览器返回来评价,你可以搜索加载作为输入密码的结果文档的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 
+0

哇约翰!这对我的方式非常有用。然而,我唯一感到沮丧的是,每当程序加载它往往说“错误的帐户凭据!”每次程序打开(所以我必须单击确定)然后我可以测试程序,它完美地工作(告诉我,如果我使用了错误的凭据或权利)。每当页面加载时,它也会提示我3次,所以我也很困惑。任何想法,为什么这是? http://pastebin.com/K8Mrq8FR – 2013-03-26 07:06:03

+0

在做出决定后,尝试添加'htmlText = Nothing'(例如,清除变量的内容)。你可以发布更多的代码审查? – 2013-03-28 18:26:39

+0

嗯...试过了,它没有工作。这里是完整的代码:http://pastebin.com/MmiTJ99j我不知道它有什么需要处理的,这很容易混淆为什么它会提示我输入错误的凭证3次,但是当我输入正确的代码,它只会给我发一次消息。 – 2013-03-28 22:06:46