VBA Web scraping GetElementsByClassName

Pak Mariman

New Member
Joined
Jan 15, 2017
Messages
21
I am trying to extract an element from a website and stick it into a cell on my spreadsheet:

VBA Code:
Worksheets("Sheet1").Cells(1, 1).Value = IE.document.getElementsByClassName("apples").innerText

(in this I have dimmed the variable IE as New InternetExplorer). The navigate bit works.

The website bit that I am after looks like:

<span id="apples">here is the text I am after<br>and this text too<br></span>

I get a 438 error.

How and why, and how to change my code? I cannot use the following code:

VBA Code:
Worksheets("Sheet1").Cells(1, 1).Value = IE.document.getElementsByTagName("span")(counter).innerText

because I am probing several similar URLs and the number of the "span" tag is not consistent.

Thanks!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
getElementsByClassName returns an array and to reference the first array element you would use getElementsByClassName("apples")(0).innerText

However, your HTML shows that "apples" is an id, not a class name, so you should use:
VBA Code:
Worksheets("Sheet1").Cells(1, 1).Value = IE.document.getElementById("apples").innerText
 
Upvote 0
getElementsByClassName returns an array and to reference the first array element you would use getElementsByClassName("apples")(0).innerText

However, your HTML shows that "apples" is an id, not a class name, so you should use:
VBA Code:
Worksheets("Sheet1").Cells(1, 1).Value = IE.document.getElementById("apples").innerText
Thanks John!

I used the getElementByID as you suggested. It now gives me a 424 error. :eek:
 
Upvote 0
The error indicates that an element with id "apples" doesn't (yet) exist, either because the page is not completely loaded or the element is in a frame.

To prove the former, click Debug on the error message and press F8 to execute the yellow highlighed line again. If no error occurs it means that the page wasn't loaded when that line was first executed.

to fix this add a wait loop after the IE.navigate:

VBA Code:
    While IE.Busy Or IE.readyState <> 4: DoEvents: Wend
And then get the span element using this code:
VBA Code:
    Dim HTMLdoc As HTMLDocument
    Dim span As HTMLSpanElement
    Set HTMLdoc = IE.document
    Do
        Set span = HTMLdoc.getElementById("apples")
        DoEvents
    Loop While span Is Nothing
    Worksheets("Sheet1").Cells(1, 1).Value = span.innerText
 
Upvote 0

Forum statistics

Threads
1,222,617
Messages
6,167,079
Members
452,094
Latest member
Roberto Saveru

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