Excel VBA:选中网页上的所有复选框

时间:2018-07-12 14:31:56

标签: excel vba web

我试图通过VBA选中网页上所有可用的复选框,因为名称约定似乎并不是我可以选择的。但是我似乎什么也无法工作。我可以登录该网站并导航到我想要的网站部分,但无法克服这一障碍。任何帮助将不胜感激。以下是该网页的源代码。

       <li data-product-family="30yr"
            data-product-amortizationTerm="30"
            data-product-type="Conventional"
            data-product-amortizationType="Fixed"

        >
        <label>
        <input type="checkbox" 
            value="154232" 
            class="product-Conventional product-item" 
            data-authorized-remittance-types="ActualActual "
            />30-Year Fixed Rate - 110k Max Loan Amount</label>
        </li>

我尝试编写(编辑)...目前正在使用的代码的VBA:

Public Sub TestIE()
Dim IE As Object
Dim aNodeList As Object, i As Long


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

' You can uncoment Next line To see form results
IE.Visible = False

' Send the form data To URL As POST binary request
IE.Navigate "https://"

' Statusbar
Application.StatusBar = "Page is loading. Please wait..."

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

IE.Visible = True

Set aNodeList = IE.document.querySelectorAll("input[type=checkbox]")
If aNodeList Is Nothing Then Exit Sub
For i = 0 To aNodeList.Length
aNodeList.Item(i).Checked = True
Next i 
End Sub

1 个答案:

答案 0 :(得分:1)

您可以尝试通过以下方式获取复选框的nodeList:

IE.document.querySelectorAll("input[type=checkbox]")

您可以沿nodeList的.Length属性遍历。

例如

Dim aNodeList As Object, i As Long
Set aNodeList = IE.document.querySelectorAll("input[type=checkbox]")
If aNodeList Is Nothing Then Exit Sub
For i = 0 To aNodeList.Length -1
    On Error Resume Next
    aNodeList.item(i).Checked  = True
    On Error GoTo 0
Next i