Quick support - VBA chrome Web scrape by element ID

Status
Not open for further replies.

VBANewbie123

New Member
Joined
Apr 18, 2015
Messages
19
Hi, all.

I have some code to open a webpage and I need to extract the inner text (if that's the correct term) of an element.

I wish to use chrome, firefox or Edge

VBA Code:
<Div id = "xxxx"

Once copied, i need to go to the excel cells(I,2).paste (or something similar)

Any help, please?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You need to install in your PC the “Selenium environment”, as described here: extract data from web to inside sheet without open website &refersh

When the environment is ready try using a macro like this one:
Code:
Sub myCaller()
Dim WPage As Object, myUrl As String
'
'Crea Driver:
'    Set WPage = CreateObject("Selenium.EdgeDriver")
    Set WPage = CreateObject("Selenium.CHRomedriver")
'
Sheets("mySheet").Select                '<<< The sheet for the result
myUrl = "https://www.your.site.html"    '<<< YOUR Url
'
WPage.Get myUrl
WPage.Wait 500
'The page is ready 
'Get the text:
Range("A2").Value = WPage.FindElementById("xxx").Text       'Your Id
'
'more instruction for more data to fetch
'
'Closing session:
WPage.Quit
Set WPage = Nothing
End Sub
This will get the element innertext into A2 of the active sheet
 
Upvote 0
This code is very good and works but it only shows one data.

I use this code:
Range("A2").Value = WPage.FindElementByClass("xxx").Text

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!
 
Upvote 0
@pecsenye: It would have been better if you had posted your question in a new thread
Anyway...
In this case you have to collect "the collection" not the single element; for example:
VBA Code:
Dim AColl As Object, I As Long
'
Set AColl = wpage.FindElementsByClass("xxx")
For I = 1 To AColl.Count
    Cells(I + 1, "A").Value = AColl(I).Text
Next I

Try...
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,215,344
Messages
6,124,407
Members
449,157
Latest member
mytux

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