Reputation: 27
I'm trying to find the fields to fill in some data online. first I wrote it in IE, what works, but is sow and hideous.
IE code
'For Internet Explorer
With IE
.Navigate URL
.Visible = True
End With
'Wait till IE is ready
Do While (IE.Busy Or IE.READYSTATE <> 4)
Sleep 3000
Loop
'Login
Set DZ = IE.Document.GetElementsByName("ctl00$ContentPlaceHolderMainNoAjax$txtClubID")
If Not DZ Is Nothing Then
DZ(0).Value = DropZone1
End If
Set UN = IE.Document.GetElementsByName("ctl00$ContentPlaceHolderMainNoAjax$txtUsername")
If Not UN Is Nothing Then
UN(0).Value = UserName
End If
Set PW = IE.Document.GetElementsByName("ctl00$ContentPlaceHolderMainNoAjax$txtPassword")
If Not PW Is Nothing Then
PW(0).Value = PassWord
End If
Set LogIn = IE.Document.GetElementsByName("ctl00$ContentPlaceHolderMainNoAjax$btnLogin")
For Each btn In LogIn
If btn.Value = "Login" Then
btn.Click
Exit For
End If
Next btn
I can't find anything similar for an MSXML2.XMLHTTP object so I'm stuck on
s = s & "Dim oXL, oXML" & vbCrLf
s = s & "Set oXL = GetObject(, ""Excel.Application"")" & vbCrLf
s = s & "Set oXML = WScript.CreateObject(""MSXML2.XMLHTTP"")" & vbCrLf
s = s & vbCrLf
s = s & "' Navigate to APF website" & vbCrLf
s = s & "oXML.Open ""GET"", ""http://www.apf.asn.au/club/login.aspx"", False" & vbCrLf
s = s & "Wscript.Sleep 50" & vbCrLf
s = s & "oXML.send" & vbCrLf
s = s & "Wscript.Sleep 50" & vbCrLf
VBA is generating this code, so I can execute it multiple times, and simulating multi-thread like that. that idea is from: Daniel Ferry, http://excelhero.com/blog
Upvotes: 0
Views: 2846
Reputation: 16311
I'm not exactly sure what you're asking. When you retrieve a web page via a GET
using XMLHTTP
, you're just downloading a static copy of the HTML/XML. There's no host to fire events. You can't click buttons or use DOM methods to find elements. All you have is a string.
If you need it to behave like a true webpage, you'll need to load the string into a DOM document. You can save your XML response string to an HTML file and then load it using IE:
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "c:\test.htm"
Set DZ = IE.Document.GetElementsByName("ctl00$ContentPlaceHolderMainNoAjax$txtClubID")
Or, if you just need to search for DOM elements, you can fake a document by writing your XML response string to an HTMLFILE
object.
Set doc = CreateObject("HTMLFILE")
doc.Write strResponse
Set DZ = doc.GetElementsByName("ctl00$ContentPlaceHolderMainNoAjax$txtClubID")
Upvotes: 2