【发布时间】:2014-07-26 13:19:19
【问题描述】:
有人可以帮助我,或者至少可以为我指明正确的方向。
我正在尝试在 VBA 中编写一个自动化脚本,该脚本将登录一个网站并点击该网站上的一些按钮和复选框,然后触发下载。
网页正在使用 ASP.net。
我可以通过使用 InternetExplorer.Applicaion 对象来实现我想要的,但问题是我使用 API 和 SendKeys 将文件保存到 SaveAs 对话框窗口,它可以工作但非常粗糙并且很容易失败并且我真的希望它能够自行运行而不会出现任何问题。
理想情况下,我希望能够使用 HTTP 请求/响应方法,但是由于站点身份验证,我已经尝试了几个小时而没有任何成功,而且我还认为它对脚本有一些保护,因为 cookie 文件具有 HttpOnly标志设置。
我要下载的文件是 text/csv 并且可以通过我可以通过 Fiddler 看到的 http 响应标头访问输出的数据。
有什么方法可以从 InternetExplorer.Applicaion 对象中获取 HTTP 响应标头,我一直在查看 MS 文档,但没有看到任何明显的方法?
这是我目前使用的代码:
Private Function GetFile() As Scripting.File
On Error Resume Next
Dim objFso As Scripting.FileSystemObject: Set objFso = New Scripting.FileSystemObject
Dim strFileName As String: strFileName = Year(Now()) & Month(Now()) & Day(Now()) & Hour(Now()) & Minute(Now()) & Second(Now()) & ".csv"
Dim strFolder As String: strFolder = objFso.GetAbsolutePathName(Environ("TEMP")) & "\"
Dim strSaveAsFullPath As String: strSaveAsFullPath = strFolder & strFileName
Dim objIE As InternetExplorer: Set objIE = New InternetExplorer
With objIE
.Visible = True
.Navigate2 "WEB URL 1"
Do Until Not .Busy
Application.Wait Now + TimeValue("00:00:01")
DoEvents
Loop
.Document.getElementById("ctl02_txtUserId").value = "USER"
.Document.getElementById("ctl02_txtPassword").value = "PASS"
.Document.getElementById("ctl02_btnLogon").Click
Do Until Not .Busy
Application.Wait Now + TimeValue("00:00:01")
DoEvents
Loop
.Navigate2 "WEB URL 2"
Do Until Not .Busy
Application.Wait Now + TimeValue("00:00:01")
DoEvents
Loop
.Document.getElementById("ddlTables").selectedIndex = 8
.Document.forms(0).submit
Do Until Not .Busy
Application.Wait Now + TimeValue("00:00:01")
Loop
.Document.getElementById("gvFields_lnkbtnSelectAll").Click
Do Until Not .Busy
Application.Wait Now + TimeValue("00:00:01")
DoEvents
Loop
.Document.getElementById("btnRetrieveData").Click
Do Until Not .Busy
Application.Wait Now + TimeValue("00:00:01")
DoEvents
Loop
.Document.getElementById("btnRetrieveData").Focus
'''''''''''''''''''''''''''''''''''''''''''' ''''''''''' '这是我使用 SENDKEYS 下载文件的地方 :( '我需要一个更好的解决方案吗??? ''''''''''''''''''''''''''''''''''''''''''''' ''''''''
Application.Wait Now + TimeValue("00:00:01")
DoEvents
Application.SendKeys "{TAB}", True
Application.SendKeys "{TAB}", True
Application.SendKeys "{DOWN}", True
Application.SendKeys "{DOWN}", True
Application.SendKeys "a", True
Application.Wait Now + TimeValue("00:00:01")
DoEvents
Application.SendKeys strSaveAsFullPath, True
Application.SendKeys "{TAB}", True
Application.SendKeys "{TAB}", True
Application.SendKeys "{TAB}", True
Application.Wait Now + TimeValue("00:00:01")
DoEvents
Application.SendKeys "s", True
Application.Wait Now + TimeValue("00:00:02")
Application.SendKeys "y", True
'''''''''''''''''''''''''''''''''''''''''''' '''''''''''
.Quit
End With
Dim objFile As Scripting.File: Set objFile = objFso.GetFile(strSaveAsFullPath)
Set GetTransworldFile = objFile
Set objIE = Nothing
Set objFso = Nothing
结束函数
【问题讨论】:
-
对于下载,如果CSV文件可以通过它自己的URL来识别,那么你可以使用一些WinAPI函数(urldownloadtofile)。如果它是一个普通的“另存为”对话框,而不是由 javascript 函数提供的,那么您可以使用其他 WinAPI 函数来获取对话框窗口的句柄并在没有 SendKeys 的情况下执行保存。如果它是一个函数服务的弹出窗口,我不知道任何其他方法。
标签: vba internet-explorer