Search code examples
vbaexcelhrefgetelementsbytagname

VBA internet explorer data extract


I have a VBA macro that I'm accessing IE through. Then, I am looping through all the links on a site using doc.getElementsByTagName("a"), and for each link, I'm able to pull: .innertext .classname .id .href .title

BUT, there are other datapoints listed within the "a" tag when I check "Inspect Element" in internet explorer. The two items I'm interested in are named "data-articletypeid" and "data-researchid." How would I pull these values? They seem like unique items for this website.

Thanks,


Solution

  • Loop through the elements of the <a> tag, then loop through the attributes of the element and extract the attribute name through nodeName.

    Dim elements, element, attr
    Dim item As String
    
    Set elements = ie.Document.GetElementsByTagName("a")
    
    For Each element In elements
        For Each attr In element.Attributes
            Debug.Print attr.nodeName
        Next attr
    Next element
    

    "The Element interface represents an element in an HTML or XML document. Elements may have attributes associated with them; since the Element interface inherits from Node, the generic Node interface attribute attributes may be used to retrieve the set of all attributes for an element..."

    https://msdn.microsoft.com/en-us/library/hh869681(v=vs.85).aspx