Search code examples
vbaexcelinternet-explorerie-automation

Automate click(expanding) plus signs within internet explorer


I am working on automating some data entry into an intranet web page. I have had success with this type of code in the past to click checkboxes, but have been unable to make it work on the plus signs that expand the rows. The below code does nothing, no error is prompted either, the code just runs it's course.

Here is my code:

Set div = IE.document.getElementsByTagName("div")

For Each i In div
    'showExpand?
    If i.id Like "iconShowA*" Then
        If i.onclick = "showExpand(*)" Then
            i.Click'Click plus sign
            v = Replace(i.id, "iconShowA", "")
            col.Add v 'store the numeric part
        End If
    End If
Next i

For Each v In col
    Debug.Print v
Next v

The pertinent HTML lines are:

(What I'm trying to click, there can be a variable number of these with a different numerical identifier "iconShowA(x)")

<div id="iconShowA34" class="ui-icon ui-icon-plusthick" onclick="showExpand('34','34')" ;="" style="display: block;"></div>

(I also need to avoid clicking these)

<div id="iconShowA_N4" class="ui-icon ui-icon-plusthick" onclick="showExpandSub('4','4')" ;=""></div>

Solution

  • The code below was able to achieve desired results. I was unable to make the TagName convention work. This method uses getElementByID to navigate through the webpage. It seemed crucial that the full ID be used, so I used the Do While loop to iterate through numbers that were possible numbers used in the ID naming convention.

    n = DateDiff("ww", firstDate, secondDate)'Determines number of plus' to click
    v = 0 'Counter for plus click event
    x = 6 ' starting value for numerical piece of Id
    
    
    Do While n > v 'continue loop until all plus' have been clicked
    Set div = IE.document.getElementById("iconShowA" & x) 'Id must be defined completely to click
        If div Is Nothing Then 'tests if element exists
            x = x + 7
        Else
            div.Click 'clicks an element that exists
            v = v + 1
            x = x + 7 'iterates id number by 7 as that is convention of website
        End If
    Loop