Solution 1 :

You css selector is wrong so no results.

Switch to the shorter


or use

span.awardings-bar > .awarding-link

Note the class selector . in front of awarding-link.

To avoid the comments being retrieved and limit just to awards section at top use a parent which restricts, for example:

.entry .awarding-link

Problem :

I’m trying to extract an attribute’s value from a few lines of HTML (of this Reddit page), do so for a few elements each, and then put those values in an array. (Basically to calculate the # of awards of a Reddit post.)

The code is not returning anything; awardCount is 0.

The goal: Check if there is a class called “awarding-link”, and add up the # of different awards to get the total amount.

I have another issue: I need to check for that “awarding-link”, but without the comment area. So they need to be child elements of the id “siteTable”. The issue is I can’t just use another “>” because that’s for immediate child elements.

Sub GetData2()

Dim IE As InternetExplorer
Dim itemEle As Object, datacountobj As Object, awards As Object
Dim upvote As Integer

Dim postdate As String, upvotepercent As String, awardAdd As String, _
  awardCount As String, animated As String, dates As String, isanimated As String, _
  oc As String, filetype As String, linkurl As String, myhtmldata As String, _
  visiComments As String, totalComments As String, removedComments As String, _
  test As String, tested As String, test2 As String, test3 As String, title As String, _
  deletedComments As String

Dim y As Integer, x As Integer
Dim U As Long, L As Long, j As Long

Set IE = New InternetExplorer
IE.Visible = True

IE.navigate (ActiveCell.Value)
Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop

Dim nodeList As Object, i As Long, urls(), results(), results2()

    Set awards = IE.document.querySelectorAll("span.awardings-bar > awarding-link")
    If Not awards Is Nothing Then
        awardCount = 0
        For x = 0 To awards.Length - 1
            awardAdd = awards.getAttribute("data-count")
            awardCount = awardCount + awardAdd
        Next x
        awardCount = 0
    End If
    results(i + 1, 14) = awardCount

(results is just a 1D 0-based array)


Comment posted by SMAKSS

In the link that you provided, I can’t find any class with the

Comment posted by Bloggy

Thanks @QHarr ! Oh, I forgot the dot. How did I not notice that. One last question: do you know how I can make sure it doesn’t find elements that are in the comment section? As in, avoid elements that are child elements of the class “commentarea” ? Because sometimes comments get awards and then they have the exact same class and parent element, so they’d get counted in. I can’t figure it out because “commentarea” isn’t an immediate parent, it’s a few levels up.

Comment posted by Bloggy

sorry to disturb you one last time (hopefully), but would you know why my awards.getAttribute(“data-count”) returns an error 438 – “Object doesn’t support this property or method” ? awards is set like this.

Comment posted by Bloggy

Nevermind, I managed to get it right! Cheers, and thanks for all the help!


Leave a Reply

Your email address will not be published. Required fields are marked *