Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vba IE object - how to find all element id's?

Status
Not open for further replies.

bartrein

Programmer
Sep 22, 2008
49
EU
HI

I found a very useful piece of code which allows you to extract information from websites and return it into your spreadsheet as long as you know the element ID of the field from the HTML document. The code below works on the yahoo finance website where at the moment it navigates to Vodafone stock and returns a price by using HTMLDoc.getElementById("yfs_l10_" &stocksymol).innerText method.

I would like to modify the code to extract info from other websites however i don't know how to find my element ID which i would put in place of "yfs_l10_".

Could anyone drop me a snippet or a link which would show how to get a list of all the elemements and their ID's for a particular website?

many thanks

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub mainMacro()

showWebsite "
End Sub


Sub showWebsite(ByVal URL As String)

Dim IEApp As InternetExplorer 'Need a reference to Microsoft Internet Controls

Dim HTMLDoc As HTMLDocument 'Need a reference to Microsoft HTML Object Library



Set IEApp = New InternetExplorer

IEApp.Visible = True


IEApp.Navigate URL

Do
DoEvents
Loop Until IEApp.ReadyState = READYSTATE_COMPLETE

Set HTMLDoc = IEApp.Document

getYahooStockQuote HTMLDoc, "vod"

IEApp.Quit

Set IEApp = Nothing

End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''

Sub getYahooStockQuote(HTMLDoc As HTMLDocument, StockSymbol As String)

Debug.Print HTMLDoc.getElementById("yfs_l10_" & StockSymbol).innerText

End Sub
 
Hi,

Don't know. But I would start by looking at the HTMLDoc object to find an element collection. For instance if the elements collection were Elements then I'd deduce that...
Code:
dim elm as element
for each elm in HTMLDoc.Elements
   msgbox elm.name
next
Then look for the properties of the Element object; like name or value or text.

Just an educated guess.


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Actually, in the HTML Object Library there is a DocumentElement property of the HTMLDocument and then there is a class, HTMLElementCollection.

Check the Object Browser.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Skip,

can you elaborate on this a bit more? How can I use the HTMLElementCollection to step through each field on my internet page to tell me the name of it so I can refer to it during a log-in process.

I tried the "Dim elm as element" suggestion you made but element isnt even a reference i can refer to.

Thanks for your help!!
 


"Dim elm as element" was a duduction (guess).

You'll notice that I followed up with something a tad more specific than a deduction.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
yes, and I didnt understand how to use that in code. Can you easily show me how you would use the HTMLElementCollection to do that? Thanks
 
You're going to have to do some research and maybe some trial and error, because I have never used this library.

In general a collection is something like an array. For instance, in Excel there is a Worksheets collection of Worksheet objects. You can loop thru that collection like...
Code:
dim ws as Worksheet
for each ws in ThisWorkbook.Worksheets
  msgbox ws.name  'display each worksheet name in thisworkbook
next


Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi Everyone

I am glad to see my old thread revived :)
and i am even more happy to be able to help now (hopefully).

Please find below a quick and dirty way of downloading all the element id's with their inner text from the URL to the spreadsheet.

Go to your website - find a piece of data (string, value) that you are interested in (this will later appear in innertext property)
Run the below code to dwonload all elemment id's.
CTRL + F to find your data will take you to the row with element id.

In this example i was looking for the "last_price" (at the moment of download 750 row 19).

Let me know if it works for you.

-----------------------------------------------------------

Sub main()

'replace with your URL
Getid "
End Sub


Function Getid(ByRef sUrl As String)

Dim IE As InternetExplorer
Dim doc, element
Set IE = New InternetExplorer

IE.Visible = True
IE.Navigate sUrl

Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE

Set doc = IE.Document
Dim i As Integer
i = 1

'loop through all elements
For Each element In doc.all

'print the ones with a name together with corresponding inner text
If Len(element.ID) <> 0 Then

Cells(i, 2) = element.innerText
Cells(i, 1) = element.ID
i = i + 1

End If


Next element

'IE.Quit

End Function
 
bartrein,

That's great. I am using in access so I am just pasting data to a table and it works like a charm to read through the mass in the 'source' of each web page. Have you tried to do anything like click a button once you've figured out the element id??

Thanks for the great info.
 
accjrf
no, i'm afraid not - are you trying to log in somewhere by clicking a button?
 
yes, enter user id, password, and then click a button. Ive found a bunch of different code out there to do this, but none of it actually works....it all fails on the "click" command they show.

thanks for your help with the above though. That is def a step in the right direction of what i am trying to accomplish.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top