如何相对地单击VBA IE自动化中的复选框(通过提供来自Excel单元的数据)?

问题描述 投票:0回答:1

我正在尝试从excel单元获取数据,例如contractID = 12345,然后通过IE自动化在网页上单击与ContractID对应的复选框。

下面是我尝试过的代码,但对我不起作用。如果有人可以帮助我,我将不胜感激。

                                <table width="100%" class="datatable" id="tblFirst">
                                  <tr>
<td class="aligncenter"><input name="j_idt312:0:chkAsset" class="assetGroup" 
id="j_idt312:0:chkAsset" onclick="selectorOnlyOneAssetGroupPlan('0')" 
type="checkbox">
                            </td>
                                    <td><span class="outputText">619014</span>
                                </tr>
                                 <tr>
                                    <td><span class="outputText">535171</span>
                                 </tr>
                               </tbody></table>

我需要获取619014,然后单击相应的复选框。此值将来自excel单元,因此首先应在网上搜索,然后选中复选框。

         contractID = Trim(Sheets("Sheet1").Range("B2"))
         Set ContractList = IE.Document.querySelectorAll("outputText").Item(i)
         For i = 0 To ContractList.Length - 1
         Debug.Print ContractList.Item(i).innerText
         If ContractList = "" & contractID & "" Then
         IE.Document.getElementById("checkbox").Click
         End If
         Next
excel vba internet-explorer automation
1个回答
0
投票

我们可以使用common VBA Methods(例如:getElementById,getElementsByTagName和getElementsByClassName方法)来查找表并遍历各行,然后找到相关的复选框和span元素,最后设置复选框checked属性。更详细的步骤,请检查以下示例代码:

VBA脚本(将网站网址更改为您的网址:

Sub Test()
    Dim IE As Object

    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = True
        .Navigate "<web site url>"

        While IE.ReadyState <> 4
            DoEvents
        Wend

        Dim contractID As String
        contractID = "535171"

        'find the table
        Set i = IE.Document.getElementById("tblFirst").getElementsByTagName("tbody")(0)
        Debug.Print i.Rows.Length
        If i.Rows.Length > 0 Then

            'loop through the table rows.
            For Each rowItem In i.Rows

                'from the second td cell, find the outputtext value and compare the value.
                If rowItem.getElementsByTagName("td")(1).getElementsByClassName("outputText")(0).innerText = contractID Then
                     'find the checkbox from the first td cell, and then checked the checkbox.
                     rowItem.getElementsByTagName("td")(0).getElementsByClassName("assetGroup")(0).Checked = True

                     'add debugger to check the checked item value
                     'Debug.Print rowItem.getElementsByTagName("td")(1).innerText

                End If
            Next rowItem
        End If
    End With
    Set IE = Nothing
End Sub

网页资源:

<table width="100%" class="datatable" id="tblFirst" >
    <tbody>
        <tr>
            <td class="aligncenter">
                <input name="j_idt312:0:chkAsset" class="assetGroup"
                       id="j_idt312:0:chkAsset"
                       type="checkbox">
            </td>
            <td><span class="outputText">619014</span>
        </tr>
        <tr>
            <td class="aligncenter">
                <input name="j_idt312:0:chkAsset" class="assetGroup"
                       id="j_idt312:0:chkAsset"
                       type="checkbox">
            </td>
            <td><span class="outputText">535171</span>
        </tr>
    </tbody>
</table>

运行VBA脚本后,输出如下:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.