0
votes

I'm trying to check all the checkboxes available on a webpage via VBA since the name convention doesnt appear to be one in which I can be selective. However I cannot seem to get anything to work. I can login to the website and navigate to the section of the website I want but cannot cross this hurdle. Any help would be greatly appreciate. Below is the source code from the webpage.

       <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 I attempted to write (edited)... code I'm using presently:

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
Your Exit For instructs the loop to exit upon the very first match.Bill Hileman
Thanks Bill, I'm somewhat new to this. I would expect that if that was the only issue it would at least check the first checkbox, which it doesnt appear to do. I get an automation error and unspecified error when running the code.iDeals
I cannot seem to get anything to work or but cannot cross this hurdle: these statements don't give us a lot to work on. Are you getting an error message? what happens when you run the code? Plus, don't use a Click, you should be able to use .Checked property to set the checkbox.. unless you are working with embedded elements?Zac
See errors above. I tried changing to 'checked' from 'Click' to no avail. Exact error reads: Run-time error '-2147467259 (80004005)': Automation error Unspecified erroriDeals
Just to clarify, to set the element as checked, you would set it as: objElement(i).Checked = TrueZac

1 Answers

1
votes

You can try to get a nodeList of the checkboxes with:

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

You can traverse the nodeList along its .Length property.

E.g.

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