I’m trying to extract values stored inside different buttons on the webpage.
It seems button of each variant has no name, they are just called “variant__box”, which are under “variants” div class.
As far as I can tell, values are loaded by javascript on each variant__box.
This is the website to get the data: https://www.honda.co.uk/motorcycles/range/adventure/crf1100l-africa-twin-adventure-sports/specifications-and-price.html#/
This is the code I’ve written so far
JavaScript
x
29
29
1
Dim ie As Object
2
Dim html As New HTMLDocument
3
Dim address, str As String
4
Dim jobDetailsList As Object
5
Dim jobitem As Object
6
7
Set ie = CreateObject("InternetExplorer.Application")
8
9
ie.navigate address 'the one mentioned above
10
ie.Visible = False
11
12
While ie.Busy Or ie.readyState < 4
13
DoEvents
14
Wend
15
16
Set html = ie.document
17
Set jobDetailsList = html.getElementsByClassName("variants")
18
19
For Each jobitem In jobDetailsList
20
jobitem.Click
21
str = jobitem.innerText
22
ActiveSheet.Cells(i, 5).Value = str
23
i = i + 1
24
Next jobitem
25
26
Set html = Nothing
27
ie.Quit
28
Set ie = Nothing
29
It returns nothing.
Advertisement
Answer
If you want to use the IE you can use the following code. But SIM’s suggestion is better because IE is then omitted.
JavaScript
1
31
31
1
Sub ScrapeMotorCycleData()
2
Dim ie As Object
3
Dim address, str As String
4
Dim jobDetailsList As Object
5
Dim jobitem As Object
6
Dim i As Long
7
8
i = 2
9
address = "https://www.honda.co.uk/motorcycles/range/adventure/crf1100l-africa-twin-adventure-sports/specifications-and-price.html#/"
10
Set ie = CreateObject("InternetExplorer.Application")
11
ie.navigate address 'the one mentioned above
12
ie.Visible = False
13
'The following line doesn't do what you want
14
'While ie.Busy Or ie.readyState < 4: DoEvents: Wend
15
16
'You nee a loop here to wait for loading the dynamic content
17
'Ask for the HTML part you want to scrape
18
'(No timeout included here, but it can be programmed)
19
Do
20
Set jobDetailsList = ie.document.getElementsByClassName("variant__wrapper")
21
Loop Until jobDetailsList.Length > 0
22
23
For Each jobitem In jobDetailsList
24
ActiveSheet.Cells(i, 5).Value = jobitem.innerText
25
i = i + 1
26
Next jobitem
27
28
ie.Quit
29
Set ie = Nothing
30
End Sub
31