网站通过VBA登录时抛出错误,指出“用户名或密码无效”

时间:2018-01-17 20:32:00

标签: html vba excel-vba excel

我使用下面的代码通过VBA在网站上自动登录,但它正在抛出错误说明

  

“无效的用户名或密码”

有人可以指导我在下面的代码中进行哪些更改以消除此问题吗?

Private Sub time_sheet_filling()

    Dim I As Long
    Dim IE As Object
    Dim doc As Object
    Dim objElement As Object
    Dim objCollection As Object

    ' Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True

    ' Send the form data To URL As POST binary request
    IE.Navigate "http://sanjay.com/default.aspx"

    ' Wait while IE loading...
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

        'Load the logon page
    Set objCollection = IE.Document.getElementsByTagName("input")
    I = 0
    While I < objCollection.Length
        If objCollection(I).ID = "username" Then
            ' Set text to enter
            objCollection(I).Value = "abc"
        End If
        If objCollection(I).ID = "password" Then
            ' Set text for password
            objCollection(I).Value = "123"
        End If

        If objCollection(I).ID = "submit" Then ' submit button clicking
            Set objElement = objCollection(I)
        End If
        I = I + 1
    Wend

    objElement.Click

End Sub

您提出的网站代码..请仔细检查并请求解决方案

 </script>
        <table cellspacing="0" cellpadding="0" style="margin: 0px auto 0 auto;">
            <tr>
                <td class="parrafo" style="text-align: left; vertical-align: middle; padding: 0 15px 0 0;">ACCOUNT</td>
                <td class="parrafo" style="text-align: left; vertical-align: middle; padding: 0 15px 0 0;">PASSWORD</td>
                <td>&#0160;</td>
            </tr>
            <tr>
                <td style="text-align: left; vertical-align: middle; padding: 0 15px 0 0;">
                    <input name="ctl00$MainContent$ctlLogin$_UserName" type="text" size="15" id="ctl00_MainContent_ctlLogin__UserName" accesskey="u" tabindex="60" class="login_input" />
                </td>
                <td style="text-align: left; vertical-align: middle; padding: 0 15px 0 0;">
                    <input name="ctl00$MainContent$ctlLogin$_IdBook" type="hidden" id="ctl00_MainContent_ctlLogin__IdBook" value="54,60,51,24,3,7,6" />
                    <input name="ctl00$MainContent$ctlLogin$Redir" type="hidden" id="ctl00_MainContent_ctlLogin_Redir" value="wager/welcome.aspx" />
                    <input name="ctl00$MainContent$ctlLogin$_Password" type="password" size="15" id="ctl00_MainContent_ctlLogin__Password" accesskey="p" tabindex="61" onkeyup="onkey(event, this.value, this)" class="login_input" />
                </td>
                <td style="text-align: left; vertical-align: middle;">
                    <input type="submit" name="ctl00$MainContent$ctlLogin$BtnSubmit" value="Login" id="ctl00_MainContent_ctlLogin_BtnSubmit" class="login_input" style="text-transform: uppercase;" />
                </td>
            </tr>               
        </table>
    </div>

1 个答案:

答案 0 :(得分:0)

无需遍历所有输入标记集合。如果您有ID,则可以直接引用它们。

尝试此修改:

Private Sub time_sheet_filling()

    Dim I As Long
    Dim IE As Object
    Dim doc As Object
    Dim objElement As Object
    Dim objCollection As Object

    ' Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True

    ' Send the form data To URL As POST binary request
    IE.navigate "http://sanjay.com/default.aspx"

    ' Wait while IE loading...
    Do While IE.Busy Or IE.readyState < 4
        DoEvents
    Loop

    'Load the logon page
    Dim oUser As Object, oPass As Object, oSubmit As Object
    Set doc = IE.document
    Set oUser = doc.getElementById("ctl00_MainContent_ctlLogin__UserName")
    Set oPass = doc.getElementById("ctl00_MainContent_ctlLogin__Password")
    Set oSubmit = doc.getElementById("ctl00_MainContent_ctlLogin_BtnSubmit")

    oUser.Value = "abc"
    oPass.Value = "123"
    oSubmit.Click

另外 - 注意我对你的循环所做的改变。 IE.Busy本身非常不可靠。有时,当网页仍在加载时,它会转为False。在您的行上,我添加了IE.ReadyState < 4作为可靠性的另一个因素。 4是常量READYSTATE_COMPLETE的值。