VBA chrome Web scrape by element Class

pecsenye

New Member
Joined
Jan 17, 2023
Messages
14
Hello everyone!

I found and modified a code here on the forum:

VBA Code:
Sub test()
Dim WPage As Object, myUrl As String
'
'Crea Driver:
'    Set WPage = CreateObject("Selenium.EdgeDriver")
    Set WPage = CreateObject("Selenium.CHRomedriver")
'
Sheets("mySheet").Select               
myUrl = "https://www.your.site.html"    
'
WPage.Get myUrl
WPage.Wait 500
'The page is ready
'Get the text:
Range("A2").Value = WPage.FindElementByClass("xxx").Text       

'Closing session:
WPage.Quit
Set WPage = Nothing
End Sub

This code is very good and works but it only shows one data.

I can see all the data of this div in A2 cell. That's great, but what if I want more of the same div?

Webpage source code:
<div class="xxx">...</div>
<div class="xxx">...</div>
<div class="xxx">...</div>
<div class="xxx">...</div>
<div class="xxx">...</div>

if i use this code i can see the first "xxx" data in A2. It's OK, but I would like to see the second xxx in A3, the third xxx in A4...
How do I do this?

Thanks for helping!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
VBA Code:
Dim divs As Object, i As Long
Set divs = WPage.FindElementsByClass("xxx")      
For i = 1 To divs.Count
    Range("A1").Offset(i).Value = divs(i).Text
Next
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,348
Members
452,638
Latest member
Oluwabukunmi

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