So I finally have to ask this from an expert after I found totally stuck in it. Here is the Macro Written by me. The main purpose is to fetch the Product Price and Product Name from Amazon Website and paste it in Excel column. All i need to add ASIN number of Product in column A1 manually. It will automatically go to the given link and collect data and paste it in respective columns i.e., column B1 and C1.
This is the example URL. I am trying to get the data from this URL for now.
I tried to find the class of Price and Name in InspectElement, but I didn't find any class or ID working there. When I run macro it goes to site and loads for sometime but it doesn't fetch Product Price or Name from there and its totally unpredictable why it's not working.
I tried using getElementsbyClassName("")
and getElementsbyID("")
. All not working.
Is there any error ( logical ) in my code or I need to rewrite it? Actually I am building a macro. Any help from Experts is very appreciable. I have Microsoft HTML Object Library & Internet Controls Checked.
Here is the code i am using:
Sub useClassnames()
Dim element As IHTMLElement
Dim elements As IHTMLElementCollection
Dim IE As InternetExplorer
Dim html As HTMLDocument
Set IE = New InternetExplorer
IE.Visible = True
IE.navigate "https://www.amazon.co.uk/Alessi-Colombina-Soup-Plate-FM10/dp/B0012620YA/ref=sr_1_1?dchild=1&keywords=B0012620YA&qid=1603405464&sr=8-1"
'Wait until IE has loaded the web page
Do While IE.readyState <> READYSTATE_COMPLETE
Application.StatusBar = " Loading Web page … "
DoEvents
Loop
Set html = IE.document
Set elements = html.getElementsByClassName(" priceblock_ourprice ")
'Here is the className "priceblock_ourpirce" I found it in InspectElement but its not getting any data. I am not able to find the 'correct ClassName for Price and Name of Product from InspectElemnt. As I am not a web developer.
Dim count As Long
Dim erow As Long
count = 0
For Each element In elements
If element.className = “priceblock_ourprice” Then
erow = Sheet1.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
Cells(erow, 1) = html.getElementsById(“priceblock_ourprice”)(count).innerValue
Cells(erow, 2) = html.getElementsById(" productTitle ")(count).innerText
count = count + 1
End If
Next element
Range("A2:B2").Select
Columns("A:A ").EntireColumn.AutoFit ""
Columns("B:B").ColumnWidth = 36
MsgBox count
End Sub