VBA错误:尝试.getElementById时需要“运行时错误'424'对象

时间:2014-09-01 15:25:48

标签: html excel vba excel-vba web-scraping

运行此代码时,我仍然遇到上述错误。我已经尝试过几十个网页搜索和调整来修复但不成功。如果非常感谢,代码如下,任何帮助。

Public Sub Tagg()

    Dim URL As String
    Dim ie As SHDocVw.InternetExplorer 'MICROSOFT Internet Controls (shdocvw.dll)
    Dim HTMLdoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library
    Dim loginFrame As HTMLIFrame
    Dim usernameInput As HTMLInputElement, passwordInput As HTMLInputElement
    Dim username As String, password As String

    username = "MTorres" 'CHANGE THIS
    password = "melissa1" 'CHANGE THIS
    URL = "https://webaccess.tagglogistics.com/cadence/webaccess.net?action=203&Full=Y&args=415878"

    Set ie = New SHDocVw.InternetExplorer

    With ie
        .Visible = True
        .navigate URL
        Do While .readyState <> READYSTATE_COMPLETE Or .Busy
            DoEvents
        Loop
        Set loginFrame = .document.getElementById("loginframe")  ' ****ERROR HERE****
        Set HTMLdoc = loginFrame.contentWindow.document

         '<input name="username" id="uname" class="ti" maxlength="32" onchange="setUserValue();"
         'onkeydown="setupUserValue(event);" onmouseup="return false;" onclick="SelectAll();" onfocus="SelectAll();"
         'aria-describedby="cof_username errormsg" type="text">

        Set usernameInput = HTMLdoc.getElementsByName("username")(0)
        usernameInput.Focus
        DoEvents
        usernameInput.Value = username
        usernameInput.FireEvent "onkeydown"
        usernameInput.FireEvent "onchange"
        usernameInput.FireEvent "onmouseup"

         '<input id="cofisso_ti_passw" name="password" class="ti" maxlength="32" aria-describedby="pass" type="password">

        Set passwordInput = HTMLdoc.getElementsByName("password")(0)
        passwordInput.Focus
        passwordInput.Value = password

         'HTMLdoc.forms(0).submit
         '<input src="/resources/images/btn_login.gif" alt="Login" title="Login" name="cofisso_btn_login" id="cofisso_btn_login" type="image">
        HTMLdoc.getElementById("cofisso_btn_login").Click
        Do While .readyState <> READYSTATE_COMPLETE Or .Busy
            DoEvents
        Loop

         '----------- NEW CODE --------------

         'Might need this wait loop
        While .document.readyState <> "complete"
            DoEvents
        Wend

         'Either reload HTMLdoc from current IE.document:
        Set HTMLdoc = .document

         'Or if LNKLOGOUT is inside an iframe:
         '        Dim iframe As HTMLIFrame
         '        Set iframe = .document.getElementsByTagName("IFRAME")(0)        '0 = 1st iframe
         '        Set HTMLdoc = iframe.contentWindow.document

         'HTMLdoc should now be available here - display webpage TEXT TO verify

        MsgBox HTMLdoc.body.innerText

         '---------- END OF NEW CODE ----------

         'Click "Sign Out" link
         '<a id="LNKLOGOUT" class="logout" href="https://servicing.capitalone.com/C1/SelfService/CMLogoutIntercept.aspx">Sign Out</a>
        HTMLdoc.getElementById("LNKLOGOUT").Click
        Do While .readyState <> READYSTATE_COMPLETE Or .Busy
            DoEvents
        Loop

    End With

End Sub

2 个答案:

答案 0 :(得分:1)

CSS selectors

通过使用CSS选择器定位感兴趣的元素,您可以缩短大部分时间。

选择器是:

  1. input[name='LoginID']
  2. input[name='Password']
  3. input[type='image']

这些说带有input标签的选择元素具有属性nametype,其值分别为'Login''Password''image'。 “ []”是属性的选择器。


VBA:

您可以通过document的{​​{3}}方法来应用CSS选择器:

.document.querySelector("input[name='LoginID']").Value = USERNAME
.document.querySelector("input[name='Password']").Value = PASSWORD
.document.querySelector("input[type='image']").Click

代码:

Option Explicit
Public Sub Tagg()
    Dim URL As String, ie As SHDocVw.InternetExplorer, HTMLdoc As MSHTML.HTMLDocument
    Const USERNAME As String = "MTorres"
    Const PASSWORD As String = "melissa1"
    URL = "https://webaccess.tagglogistics.com/cadence/webaccess.net?action=203&Full=Y&args=415878"

    Set ie = New SHDocVw.InternetExplorer

    With ie
        .Visible = True
        .navigate URL
        Do While .readyState <> READYSTATE_COMPLETE Or .Busy
            DoEvents
        Loop

        .document.querySelector("input[name='LoginID']").Value = USERNAME
        .document.querySelector("input[name='Password']").Value = PASSWORD
        .document.querySelector("input[type='image']").Click

        Do While .readyState <> READYSTATE_COMPLETE Or .Busy
            DoEvents
        Loop

        Set HTMLdoc = .document
        'Other code
        .Quit
    End With
End Sub

答案 1 :(得分:0)

您没有正确设置文档和登录框架。

替换这些行:

Set loginFrame = .document.getElementById("loginframe")  ' ****ERROR HERE****
Set HTMLdoc = loginFrame.contentWindow.document

有了这些:

Set HTMLdoc = ie.Document
Set loginFrame = HTMLdoc.getElementById("loginframe")  ' ****ERROR FIXED****

当您尝试查找名称为&#34; username&#34;的元素时,您会收到几行错误。因为该页面上没有这样的元素,但希望这会让你走上正轨。