Creating a HTML DOM object for accessing HTML objects in a html form
Solution 1
There is no "HTMLDOM" object, since there is a lot more attached to HTML than to XML. It would take JavaScript handling, session handling, CSS handling, HTTP requests, cookie handling, caching etc to turn textual HTML into a meaningful in-memory document object.
If all that was implemented, you'd have a complete browser. Which is why there is no such COM object.
For your task could could use the Internet Explorer directly through COM automation:
Option Explicit
Dim IE, queryField
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://www.google.com"
While IE.Busy Or IE.readyState <> 4
WScript.Sleep 100
Wend
Set queryField = GetFormFieldByName(IE.document, "q")
If Not queryField Is Nothing Then
QueryField.value = "test"
QueryField.form.submit
End If
WScript.Sleep 5000
IE.Quit
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function GetFormFieldByName(Parent, FindName)
Dim FormFields, FormField
Set GetFormFieldByName = Nothing
Set FormFields = Parent.getElementsByTagName("INPUT")
For Each FormField In FormFields
If UCase(FormField.Name) = UCase(FindName) Then
Set GetFormFieldByName = FormField
Exit For
End If
Next
End Function
Solution 2
Set oDoc = CreateObject("HTMLFILE")
oDoc.write "<html><head><title></title></head><body><div id='div'>hello</div></body></html>"
Response.Write oDoc.getElementById("div").innerHTML
--
WScript.Echo oDoc.getElementById("div").innerHTML
Output: -
hello
user1925406
Updated on June 05, 2022Comments
-
user1925406 about 2 years
Could any one help me in creating a HTML DOM object using VB script. I have to navigate through a HTML form and enter value in the textbox or select a value from a drop down using vb script and HTML DOm functions.
I know that to create a XMl dom object we can use a below statement, so any statements simillar to below one is available to create a HTML DOM.
Set Xmlobj = CreateObject ("Microsoft.XMLDOm") Set Htmlobj = CreateObject ("Microsoft.HtmlDom") ' Is this avalibale when I tried it shows error for object creattion, other workaround available.
-
user1925406 over 11 yearsThanks a Lot!!! one doubt, in this approach we are using the automation object model of internet explorer to create a IE object. And we are combining with HTML DOM properties and functions.My doubt is can we chain any object or property (of HTML DOm) with createdIEobject? My second doubt is why do we chain IE with document property and this 'document' propery or object used here is a HTML DOm or IEs automation object model.
-
Tomalak over 11 yearsThe
IE.document
is exactly the samedocument
object you see from JavaScript running inside the browser. With it you can perform any operation IE supports. That's why it's called "automation": you use an external script to remote control IE. -
user1925406 over 11 yearsokay, I could understand that only using IE.document we can call other methods and properties of HTML DOM. But this term "document" here is it an object, method or property?
-
Tomalak over 11 yearsIt's a property of the IE object. What difference does that make?