I've been doing Google detective work for a few hours (including searching StackOverflow) for a technique to allow targeting of HTML elements produced by JavaScript in VBA.
As an example, I cannot use ie.Document.getElementById(id) on this: .html?page=quote&sym=NGU12&mode=i
However, that method is quite capable of finding elements on static pages, as tested on google.
I've played with simply getting all the InnerText from Document.body and then parsing the file for my desired TD values, but I ran into a roadblock when trying to split two values that are on different lines. As an illustration, the following was understood by Split(..) as "2040":
20
40
From what I understand, the problem with getElementById(id) is that the website's table is generated by JavaScript after the page has loaded, and thus any elements created by that JavaScript cannot be targeted by my VBA code. Is there anyway to have my VBA code see this JavaScript-generated content?
Thank you for your help!
Edit
The VBA code being used:
Function Quote(Market, Parameter)
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate ".html?page=quote&sym=NGU12&mode=i"
While ie.Busy
DoEvents
Wend
Dim id As String
id = "dt1_" & Market & "_" & StrConv(Parameter, vbLowerCase)
Quote = ie.Document.getElementByID(id).InnerText
ie.Quit
End Function
Basically I try to build an HTML element ID using the column header for the Market. So if, for example the market is "NGU12" and the column header is "Open", the ID built is: "dt1_NGU12_open", which is the ID for the TD element containing the "Open" value for market NGU12.
I've been doing Google detective work for a few hours (including searching StackOverflow) for a technique to allow targeting of HTML elements produced by JavaScript in VBA.
As an example, I cannot use ie.Document.getElementById(id) on this: http://www.kisfutures./electronic.html?page=quote&sym=NGU12&mode=i
However, that method is quite capable of finding elements on static pages, as tested on google..
I've played with simply getting all the InnerText from Document.body and then parsing the file for my desired TD values, but I ran into a roadblock when trying to split two values that are on different lines. As an illustration, the following was understood by Split(..) as "2040":
20
40
From what I understand, the problem with getElementById(id) is that the website's table is generated by JavaScript after the page has loaded, and thus any elements created by that JavaScript cannot be targeted by my VBA code. Is there anyway to have my VBA code see this JavaScript-generated content?
Thank you for your help!
Edit
The VBA code being used:
Function Quote(Market, Parameter)
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.kisfutures./electronic.html?page=quote&sym=NGU12&mode=i"
While ie.Busy
DoEvents
Wend
Dim id As String
id = "dt1_" & Market & "_" & StrConv(Parameter, vbLowerCase)
Quote = ie.Document.getElementByID(id).InnerText
ie.Quit
End Function
Basically I try to build an HTML element ID using the column header for the Market. So if, for example the market is "NGU12" and the column header is "Open", the ID built is: "dt1_NGU12_open", which is the ID for the TD element containing the "Open" value for market NGU12.
Is there a way to have JavaScript write the necessary elements for Document.getElementById(..) before it is called?
Write where? Before what is called?
– Zhihao
Commented
Aug 5, 2012 at 22:34
innerText
will be tricky. For example, Firefox does not support innerText
, and uses the W3C standard textContent
instead. Whitespace will also be treated differently (e.g. You won't get line breaks with FF textContent
), and you will get the script
tag contents as well. Apparently Opera (I haven't tried it myself) has innerText
, but it's treated as textContent
. I feel the only reliable way of getting consistent content is with innerHTML
, but then you will be parsing markup, which is a whole other problem.
– Zhihao
Commented
Aug 5, 2012 at 22:38
As Tim has mentioned there should be no difference between statically and dynamically generated content when attempting access. Try this:
Function Quote(Market, Parameter)
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.kisfutures./electronic.html?page=quote&sym=NGU12&mode=i"
While ie.Busy
DoEvents
Wend
Application.Wait Now + TimeSerial(0,0,1) 'Pause 1 second, hopefully
'the dynamic content is
'generated within this
'time-frame
Dim id As String
id = "dt1_" & Market & "_" & StrConv(Parameter, vbLowerCase)
Quote = ie.Document.getElementByID(id).InnerText
ie.Quit
End Function
There are better ways of halting execution (see Windows Sleep API) but I'll leave that to you if you need more fine-grained control.
Also, if you'd like to test whether a specific id exists after the page has been generated you could load the page manually in IE and add a bookmarklet containing the following as the URL:
javascript:void((function() {var a = document.getElementById(prompt('Please enter an id to test','dt1_NGU12_open')); if (a) {alert('Element found');} else {alert('Element not found')};})())