通过VBA登录网站抛出错误,指出"Invalid user name or password"



我使用以下代码通过 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

结束子

您要求的网站代码..请检查并请帮助解决

 </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>

无需遍历所有输入标记集合。当您有 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作为可靠性的另一个因素。 常量READYSTATE_COMPLETE的值4

相关内容

最新更新