Two questions about getElementsByClassName()

  • Hi Everyone! I am new to webscraping and have only a little VBA knowledge.


    I am working on a webscraper, and I have made progress, but there are two questions which I can't find the answer.


    Question no. 1:


    Given these lines of code:

    Code
    1. Set my_classes = .Document.getElementsByClassName("box r")
    2. MsgBox my_classes
    3. For Each elem In my_classes
    4. Set my_il_tags = elem.getElementsByTagName("li").Item(1)
    5. If Not my_il_tags Is Nothing Then MsgBox elem.outerHTML
    6. Next elem


    For my_classes I get object: HtmlElement. Why it is not a HtmlElementCollection? It has 6 elements which I am looping trough, so how come it is not a collection? I am confused and do not understand it... Thank you


    Question no. 2:
    The same line abouve returns two items. The problem is that for the firs item the class name is: "box r" (the class I am looking for) but the other class name is: "box r properties". I am not looking for this class at all. I thought getElementsByClassName is case sensitive. If not, how could I make it to be case sensitive?


    Thank you in advance!

  • Re: Two questions about getElementsByClassName()


    Hi hunsnowboarder
    Html converts all elements to lowercase
    Xml is case-sensitive


    Code
    1. Set my_classes = .Document.getElementsByClassName("box r")
    2. MsgBox my_classes
    3. For Each elem In my_classes
    4. if elem like "box r" then
    5. Set my_il_tags = elem.getElementsByTagName("li").Item(1)
    6. If Not my_il_tags Is Nothing Then MsgBox elem.outerHTML
    7. end id
    8. Next elem
  • Re: Two questions about getElementsByClassName()


    Dear Pike,


    Thank you very much for your reply! However I changed your code to:

    Code
    1. Set my_classes = .Document.getElementsByClassName("box r")
    2. MsgBox my_classes
    3. For Each elem In my_classes
    4. If elem.getAttribute("class") = "box r" Then
    5. Set my_il_tags = elem.getElementsByTagName("li").Item(1)
    6. If Not my_il_tags Is Nothing Then MsgBox elem.outerHTML
    7. End id
    8. Next elem


    Thank you for your help!