GetElementsByName不返回任何内容

时间:2018-09-05 04:09:29

标签: vba dom web-scraping

我正在尝试使用VBA登录某些网站。我想填写用户名输入框。但是getElementsByName不返回任何内容:

Sub login()
    Const Url$ = "https://10.130.44.140:5461/hkwp/CP"

    Dim UserName As String, Password As String

    UserName = "qwe"
    Password = "qwe"

    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")

    With ie
        .navigate Url
        ieBusy ie

        On Error Resume Next
        For i = 1 To 10 'Loop ten times to ensure the certifiacate will be bypassed.
        .Document.getElementsByName("overridelink").Item.Click
        Next i
        On Error GoTo 0

       .Visible = True

        Set objInputs = ie.Document.getElementsByName("username")
        For Each ele In objInputs
            Debug.Print ele.Value
        Next

        Dim oLogin As Object, oPassword As Object
        Set oLogin = .Document.getElementsByName("username")(0)
        Set oPassword = .Document.getElementsByName("Password")(0)

        oLogin.Value = UserName
        oPassword.Value = Password
        .Document.forms(0).submit
    End With
End Sub

Sub ieBusy(ie As Object)
    Do While ie.Busy Or ie.readyState < 4
        DoEvents
    Loop
End Sub

网络资源

<body>
<form method="post" action="https://webportaluat.cigna.com.hk/pkmslogin.form">
<input type="HIDDEN" name="login-form-type" value="pwd">
<div align="center">
<div class="outer_container">
<div class="">
<table width="968px" border="0" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" vspace="0">
    <tbody><tr>
        <td width="968" height="68" bgcolor="#FFFFFF">
        <div style="float: left"></div>
        <div style="float: right" class="link_11_grey header_line">
            <a href="/hkwp/HKWPCAP/unsecure/ForwardToLoginPage.do?actionParam=forwardToLoginPage&amp;localeId=zh_HK">中文</a>
         </div>
        </td>
    </tr>
</tbody></table>
<table height="51" border="0" cellpadding="0" cellspacing="0">
    <tbody><tr>
        <td width="322" class="left_menu_nav">
        <table width="278" border="0" cellpadding="0" cellspacing="0" align="left">
            <tbody><tr>
                <td colspan="4" align="left"><img src="/hkwp/HKWPCAP/images/en/login_cignaHK.png"></td>
            </tr>
            <tr valign="left">
                <td height="10" colspan="4">&nbsp;</td>
            </tr>
            <tr valign="left">
                <td width="23">&nbsp;</td>
                <td width="71" align="left" valing="middle"><span class="font_11_grey_b">Username</span></td>
                <td width="161"><input name="username" type="text" class="login_field_frame font_11_grey"></td>
                <td width="32">&nbsp;</td>
            </tr>
            <tr valign="left">
                <td>&nbsp;</td>
                <td align="left" valing="middle"><span class="font_11_grey_b">Password</span></td>
                <td><input name="password" type="password" class="login_field_frame font_11_grey" onkeyup="javascript:forwardToLogin(event);"></td>
                <td><a href="javascript:submitForm()" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('go','','/HKWPCAP/images/en/common/com_btnGoR.gif',1)"><img src="https://webportaluat.cigna.com.hk/hkwp/HKWPCAP/images/en/common/com_btnGo.gif" name="go" width="19" height="19" border="0" id="go"></a> <img src="/hkwp/HKWPCAP/images/en/common/com_btnGoR.gif" width="19" height="19" border="0" name="goR" id="goR" style="display: none"></td>
            </tr>
            <tr>
                <td colspan="4"><img src="/hkwp/HKWPCAP/images/en/login_line.gif" width="287" height="2" vspace="10"></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td colspan="3">
                <table width="154" border="0" cellpadding="0" cellspacing="0" align="left" class="left_menu_nav_box">
                    <tbody><tr>
                        <td width="12"><img src="/hkwp/HKWPCAP/images/en/common/com_greenArrow.gif"></td>
                        <td width="142" class="link_11_blue"><a href="/hkwp/HKWPCAP/unsecure/Registration.do?actionParam=forwordToRegistration" class="font_11_grey_b">Registration</a></td>
                    </tr>
                    <tr>
                        <td><img src="/hkwp/HKWPCAP/images/en/common/com_greenArrow.gif"></td>
                        <td class="link_11_blue"><a href="/hkwp/HKWPCAP/unsecure/guideLineEn.do" target="_blank">User Guide</a></td>
                    </tr>
                    <tr>
                        <td><img src="/hkwp/HKWPCAP/images/en/common/com_greenArrow.gif"></td>
                        <td class="link_11_blue"><a href="/hkwp/HKWPCAP/unsecure/ForgotPassword.do?actionParam=forward">Forget Password</a></td>
                    </tr>
                </tbody></table>
                </td>
            </tr>
        </tbody></table>
        </td>
        <td><img src="/hkwp/HKWPCAP/images/en/HKWP_MAIN_LR.jpg"></td>
    </tr>
</tbody></table>
</div>






<div class="footer_container">
<table width="100%">
    <tbody><tr class="font_11_grey link_11_grey">
        <td width="11%" align="center">© 2012 Cigna</td>
        <td width="78%" align="center" class="footer_font"><a href="../unsecure/ConditionViewStandalone.do" target="_blank">Terms and Conditions</a> | <a href="../unsecure/Faq.do" class="footer_font" target="_blank">FAQ</a></td>
        <td width="11%"></td>
    </tr>
</tbody></table>
</div>
</div>
</div>
</form>
</body>

根据我的理解,我检查了两个输入框,它们分别带有Name用户名和密码,但是当getElement方法返回Nothing时。我也尝试打印输入框,但也没有任何效果。似乎用户名不是我应该寻找的正确索引。

1 个答案:

答案 0 :(得分:1)

有一个父form标签可以首先导航。导航之后,我使用[name='username']的CSS属性选择器通过元素的name属性和值来定位元素。

Option Explicit
Public Sub LoginPage()
    Dim IE As New InternetExplorer
    With IE
        .Visible = True
        .navigate "https://hk.member.cigna.com/hkwp/HKWPCAP/unsecure/ForwardToLoginPage.do?actionParam=forwardToLoginPage&ssoId=unauthenticated&ssoId=a4wong"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document.getElementsByTagName("form")(0) '<==Navigate parent form tag
            .querySelector("[name='username']").Value = "myUserName"
            .querySelector("[name='password']").Value = "myPassword"
        End With
        Stop                                     '<==Delete me after inspecting
        '.Quit '<== Remember to quit application
    End With
End Sub