0

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>
Community
  • 1
  • 1
Richard Lusch
  • 1,050
  • 10
  • 19
  • So what exactly is the problem? Are you getting an error? Is it not doing anything? Is it clicking the things you don't want clicked? – Bond Sep 17 '15 at 20:13

2 Answers2

0

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
Richard Lusch
  • 1,050
  • 10
  • 19
0

CSS selectors:

Assuming all the elements you want to click have showExpandSub and not showExpand then you can use a CSS selector to select these elements:

div[onclick^='showExpand(']

This says elements with div tag having attribute onclick with value starting with 'showExpand('.


CSS query:

CSS query


VBA:

Use the querySelectorAll method of document to return a nodeList of all matching elements. You then loop the .Length to retrieve elements.

Dim aNodeList As Object, iNode As Long
Set aNodeList = ie.document.querySelectorAll("div[onclick^='showExpand(']")
For iNode = 0 To aNodeList.Length - 1
    Debug.Print aNodeList.item(iNode).innerText
    'Debug.Print aNodeList(iNode).innerText '<== Sometimes this syntax
Next iNode
QHarr
  • 83,427
  • 12
  • 54
  • 101