Get attribute of a <a> element that has no ID or class (1 Viewer)

bJay

New member
Local time
Today, 04:18
Joined
Aug 29, 2014
Messages
4
Hello all;

I tried searching my question on google but couldn't find an answer I'm looking for, probably because I couldn't phrase it properly in a short sentence. So here I am...

I'm trying to access a webpage through Access VBA code, but having trouble getting the "href" form <a> element that don't have a ID or Class.

Here's the example of HTML code I'm trying to access
Code:
<div class="sub_cat_title">
<a href="http://somewebsite.com/somepage.htm">Link name</a>
</div>

Here's the VBA I tried:

Code:
Dim col As IHTMLElementCollection
Dim elm As IHTMLElement

Set col = html.getElementsByClassName("sub_cat_title")
    For Each elm In col
                field3 = elm.getAttribute("href") 
                field2 = elm.outerText
                Debug.Print field2 & ", " & field3
    Next
As you can expect it only extract the elm.outerText part (Link name) and ignores the elm.getAttribute("heref"). I don't know how to get to the <a> element.

Can someone please help?

Thank you so much for your time.
BJ :)
 
Last edited:

bJay

New member
Local time
Today, 04:18
Joined
Aug 29, 2014
Messages
4
Figured it out (for the benefit of others who may have the same issue):

instead of: elm.getAttribute("href")
use: elm.Children(0).getAttribute("href")
 

Users who are viewing this thread

Top Bottom