Extract data from a webpage and display it on a cell

shirazx3

New Member
Joined
Jul 7, 2022
Messages
23
Office Version
  1. 2021
Platform
  1. Web
Hello,
So I am a total novice with Excel VBA. I am trying to create a scenario where I can extract a particular value (inside an ID tag) from a webpage and display it on a particular cell in my excel worksheet. But it doesn't seem to work. Where am I going wrong? Thanks

Private ch As Selenium.ChromeDriver
Sub Test()

Set ch = New Selenium.ChromeDriver
ch.AddArgument "start-maximized"
ch.Start baseUrl:="https://upxland.me/users"
ch.Get ("/delnia")
Set b = ch.FindElementById("input-87")
Cells(1, 1).Value = b.Text

End Sub
 
Last edited by a moderator:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Do you receive a run-time error? Does Chrome shows the page that you expect? If you write any value in cells(1,1) daes the macro leave the cell unchanged or empty?
For debugging, add the following block of instructions:
VBA Code:
Set b = ch.FindElementById("input-87")
'ADD THIS BLOCK >>
On Error Resume Next
    Debug.Print "A", TypeName(b)
    Debug.Print "B", b.Attribute("outerHTML")
On Error GoTo 0
'<< End of block
Cells(1, 1).Value = b.Text

End Sub
Then run your code; at the end open the vba editor (from Excel Alt-F11 should do the job); open the "Immediate window" (Contr-g should o the job), copy any printout you find there and pste it into your next message, along with the answers to the initial questions
 
Last edited:
Upvote 0
Do you receive a run-time error? Does Chrome shows the page that you expect? If you write any value in cells(1,1) daes the macro leave the cell unchanged or empty?
For debugging, add the following block of instructions:
VBA Code:
Set b = ch.FindElementById("input-87")
'ADD THIS BLOCK >>
On Error Resume Next
    Debug.Print "A", TypeName(b)
    Debug.Print "B", b.Attribute("outerHTML")
On Error GoTo 0
'<< End of block
Cells(1, 1).Value = b.Text

End Sub
Then run your code; at the end open the vba editor (from Excel Alt-F11 should do the job); open the "Immediate window" (Contr-g should o the job), copy any printout you find there and pste it into your next message, along with the answers to the initial questions
Thanks for reaching out.
No there is no run-time error. Chrome opens the page that I've inserted and it opens just fine. After running the macro, there are no values in cell(1,1) meaning its still empty. If I write anything in cell(1,1) manually, it shows what I wrote.

This is what Immediate window displayed :

A WebElement
B <input id="input-87" readonly="readonly" type="text">
 
Upvote 0
A WebElement
B <input id="input-87" readonly="readonly" type="text">
A says that the element "b" is identified, but B says it has no any Text associated

I looked at the web page and I didn't understand how the value is displayed; we need someone that can suggest what to look for within the html code
 
Upvote 0
The box with value enclosed within 2 red lines is what I am trying to extract and the associated element with it. I believe the data comes from the blockchain and it is a dynamic value.
Thanks anyways for your consideration. :)
1657208334017.png

A says that the element "b" is identified, but B says it has no any Text associated

I looked at the web page and I didn't understand how the value is displayed; we need someone that can suggest what to look for within the html code
 
Upvote 0
I already had visited that page and examined those elements; as I wrote, I don't understand which trick they use to show those values, that is a question for an expert in web design
 
Upvote 0
...but I am curious enough, so continued to explore the element; it has a "value" attribute that can be catched:
VBA Code:
Cells(1, 1).Value = b.attribute("value")
 

Attachments

  • ATTRIBUTE1_Immagine 2022-07-07 183253.jpg
    ATTRIBUTE1_Immagine 2022-07-07 183253.jpg
    10 KB · Views: 11
Upvote 0
Solution
I apologize for the late response but your solution worked :)...Thank you once again...Another thing I would like to seek from you is if I can update this value anytime I want....like putting a button for refresh or whichever way possible.
Thanks

1657271329178.png
1657271353094.png
 
Upvote 0
I had another question.
1- How can I get this result without opening the browser?
2- How should I proceed if I want to extract the same data for 2 users instead of 1?
 
Upvote 0
I had no time to explore if this search works using an MSXML request, so you have to use Chrome. If you dont like it taking the full screen, you could use argument “—headless” instead of “start-maximized”:
VBA Code:
‘ch.AddArgument "start-maximized"
ch.AddArgument "--headless"

But I would recommand starting the window normally and resizing the window:
VBA Code:
WPage.Get ("/delnia")
WPage.Window.SetSize 0, 0
In this way you will see there is a chrome window, without it getting the full scene

If you have to get the same information for two users, the easiest way is that you duplicate the code from ch.Get ("/delnia"); if you have more users you could use a loop, for example:
VBA Code:
ch.Start baseUrl:="https://upxland.me/users"
ch.Window.SetSize 0, 0
uArr = Array("delnia", "pippia", "poppia")
For I = 0 To UBound(uArr)
    ch.Get "/" & uArr(I)
    ch.Wait 500
    Set B = ch.FindElementById("input-87")
    Cells(1, I + 1).Value = B.Attribute("value")
Next I

To easily start the macro you could add a button on your worksheet linkedt to your Sub Test, or you can use a "shortcut", or other ways described here: Run a macro
 
Upvote 0

Forum statistics

Threads
1,223,999
Messages
6,175,882
Members
452,679
Latest member
darryl47nopra

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