Web Scrapping - Get Element By Attribute Name ?

Dzirt07

New Member
Joined
Oct 10, 2017
Messages
11
Hello, remembers of the forum

I'm having troubles with webpage scrapping by using VBA

I know I can select element of page by using
- getElementByID
- getElementsByClassName
- getElementsByTagName

However, I have no idea how to get data by Attribute
For example
https://www.auction.com/details/2425-s-warnock-st-philadelphia-pa-19148-2573625-o_1184l


Code:
Code:
"div class="highlight-type_styles_20u0" data-elm-id="prop-type" data-reactid="170""


I can't use class because class="highlight-type_styles_20u0" is not unique

But
Code:
data-elm-id="prop-type"
is truly unique

If it was just "id" instead of "dat-elm-id" that wouldn't be a problem

Is there any way to address to HTML element by the specific name of the attribute?

Thank you for your help
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
First, use getElementsByClassName to get elements that have "field-data_styles_r_Zf" as its class name, then for each element use getAttribute to check whether "field_property_type" is its attribute. If so, use innerText to get its value. So, for example, assuming that you've assigned the object variable HTMLDoc the HTML document, try...

Code:
    Dim HTMLDoc As Object
    Dim HTMLElement As Object

    'etc
    '
    '
    
    For Each HTMLElement In HTMLDoc.getElementsByClassName("field-data_styles_r_Zf")
        If HTMLElement.getAttribute("data-elm-id") = "field_property_type" Then
            Debug.Print HTMLElement.innerText
            Exit For
        End If
    Next HTMLElement

Hope this helps!
 
Upvote 0
Solution
First, use getElementsByClassName to get elements that have "field-data_styles_r_Zf" as its class name, then for each element use getAttribute to check whether "field_property_type" is its attribute. If so, use innerText to get its value. So, for example, assuming that you've assigned the object variable HTMLDoc the HTML document, try...

Code:
    Dim HTMLDoc As Object
    Dim HTMLElement As Object

    'etc
    '
    '
    
    For Each HTMLElement In HTMLDoc.getElementsByClassName("field-data_styles_r_Zf")
        If HTMLElement.getAttribute("data-elm-id") = "field_property_type" Then
            Debug.Print HTMLElement.innerText
            Exit For
        End If
    Next HTMLElement

Hope this helps!


Thank you so much! This is great solution
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top