如何在MSXML2.XMLHTTP中查找元素

时间:2014-04-21 02:15:45

标签: vba excel-vba vbscript xmlhttprequest excel

我试图找到要在线填写一些数据的字段。首先我在IE中写了它,有什么用,但是播种和丑陋。

IE代码

'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

我无法找到类似于MSXML2.XMLHTTP对象的任何内容 所以我坚持

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正在生成此代码,因此我可以多次执行它,并模拟这样的多线程。这个想法来自:Daniel Ferry,http://excelhero.com/blog

1 个答案:

答案 0 :(得分:2)

我不确定你在问什么。当您使用GET通过XMLHTTP检索网页时,您只需下载HTML / XML的静态副本。没有举办活动的主持人。您无法单击按钮或使用DOM方法查找元素。你所拥有的只是一个字符串。

如果您需要它像真正的网页一样,您需要将字符串加载到DOM文档中。您可以将XML响应字符串保存为HTML文件,然后使用IE:

加载它
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "c:\test.htm"
Set DZ = IE.Document.GetElementsByName("ctl00$ContentPlaceHolderMainNoAjax$txtClubID")

或者,如果您只需要搜索DOM元素,则可以通过将XML响应字符串写入HTMLFILE对象来伪造文档。

Set doc = CreateObject("HTMLFILE")
doc.Write strResponse
Set DZ = doc.GetElementsByName("ctl00$ContentPlaceHolderMainNoAjax$txtClubID")
相关问题