我在 Visual Studio 的 SSIS 中使用 Visual Basic 脚本任务来自动从https://www.fincen.gov/msb-registrant-search下载 excel 文件。
我导航到该站点并单击蓝色的查看/下载链接没有任何问题。
我遇到的问题是黄色的“框架通知栏”出现时,我需要以编程方式单击“保存”。(我宁愿不使用 SendKeys 方法。)
我正在使用 UIAutomation 参考来完成此操作。
除了使用 UIAutomation 关于如何下载此文件之外,我还愿意接受其他建议 - 该解决方案必须能够用作 SSIS 包的一部分。
我已经在 Excel VBA 中编写了这段代码并且运行良好,但是我在将代码转换为 VB 时遇到了问题。
成功点击下载链接后,我运行的代码如下:
- 找到通知栏 hWnd
- 阅读通知栏中的文本以确定确切的文件名,因为它每次都会更改(我将在下载后使用该文件)
- 点击保存按钮
- 检查通知栏中的文本以确定下载何时完成。
*如果有帮助,我可以提供有效的 Excel VBA 代码。
#Region "Imports"
Imports System.Threading.Thread
Imports UIAutomationClient
Imports UIAutomationClient.UIA_PropertyIds
Imports UIAutomationClient.UIA_PatternIds
#End Region
<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
Public CUIAuto As IUIAutomation
Public WindowHandleElement As IUIAutomationElement
Public windowHandle As Long
Public tCnd As IUIAutomationCondition
Public bCnd As IUIAutomationCondition
Public tCtl As IUIAutomationElement
Public bCtl As IUIAutomationElement
Public dlStatus As String
Public InvokePattern As IUIAutomationInvokePattern
Public Sub Main()
getReport()
GC.Collect()
GC.WaitForPendingFinalizers()
Dts.TaskResult = ScriptResults.Success
End Sub
Public Sub getReport()
Dim IE As InternetExplorerMedium
Dim iePage As mshtml.HTMLDocument
Dim msbExport As IHTMLElement
IE = New InternetExplorerMedium
IE.Visible = True
IE.Navigate("https://www.fincen.gov/fcn/financial_institutions/msb/msbstateselector.html#")
Do : Sleep(1000) : Loop Until IE.ReadyState = tagREADYSTATE.READYSTATE_COMPLETE And IE.Busy = False
iePage = CType(IE.Document, mshtml.HTMLDocument)
msbExport = iePage.getElementById("ExportExcelLink")
msbExport.click()
Do
windowHandle = IE.HWND
windowHandle = CLng(FindWindowEx(windowHandle, 0, "Frame Notification Bar", vbNullString))
Sleep(1000)
Loop Until windowHandle > 0
CUIAuto = New CUIAutomation
WindowHandleElement = CUIAuto.ElementFromHandle(CType(windowHandle, IntPtr))
tCnd = CUIAuto.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_TextControlTypeId)
tCtl = WindowHandleElement.FindFirst(TreeScope.TreeScope_Subtree, tCnd)
dlStatus = CType(tCtl.GetCurrentPropertyValue(UIA_ValueValuePropertyId), String) 'get download status
bCnd = CUIAuto.CreatePropertyCondition(UIA_NamePropertyId, "Save")
bCtl = WindowHandleElement.FindFirst(TreeScope.TreeScope_Subtree, bCnd) ' The error seems to be happening here, there is no object after running this line
InvokePattern = CType(bCtl.GetCurrentPattern(UIA_InvokePatternId), IUIAutomationInvokePattern)
InvokePattern.Invoke()
End Sub
#Region "ScriptResults declaration"
'This enum provides a convenient shorthand within the scope of this class for setting the
'result of the script.
'This code was generated automatically.
Enum ScriptResults
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
End Enum
#End Region
End Class
参考: