我正在编写代码以通过模拟一系列按钮单击来自动从网站下载电子表格。(必须这样做,winHTTP 方法由于其他原因不起作用)。
该代码单击一个下载按钮,然后单击框架通知栏上的保存和打开按钮。但是,我遇到的问题是,下载按钮有时会因任何原因卡住,或者速度非常慢。在这种情况下,我用来等待下载的代码被忽略了,代码继续运行到它应该在框架通知栏中单击保存的点,它不可避免地会引发错误。
我的问题是,如何处理按钮被点击后卡住的情况?如果下载按钮卡住,可能会出现超时错误消息?尽管在我当前的代码中我不确定这将如何工作,因为它似乎忽略了我对下载是否已完成的检查。
Sub StartIE()
Dim appIE As Object
Dim URLString As String
Dim HTMLdoc, btn As Object
Dim eBrowser As IUIAutomationElement
Set appIE = CreateObject("internetexplorer.application") ' create an instance of internet explorer
With appIE
.Navigate "https://analytics.twitter.com/user/QinetiQ/tweets" 'go to this url
.Visible = True ' and show the IE
End With
apiShowWindow appIE.hwnd, SW_SHOWMINIMIZED ' minimise IE (wouldn't work with IE hidden for some reason)
Do While appIE.Busy Or (appIE.READYSTATE <> 4) ' wait until IE has finished loading
DoEvents
Loop
URLString = appIE.LocationURL
'Application.Wait (Now + TimeValue("0:00:04")) ' wait a little bit longer as waiting for IE.Busy to finish doesn't seem to work 100%
If InStr(URLString, "login") Then
UseCredentials appIE 'inputs login details for the site
Do While appIE.Busy Or (appIE.READYSTATE <> 4) ' wait until IE has finished loading
DoEvents
Loop
End If
'Application.Wait (Now + TimeValue("0:00:04"))
Set HTMLdoc = appIE.document
Set btn = HTMLdoc.getElementsByClassName("btn btn-default ladda-button")(0) 'finds the export data button
btn.Click 'clicks the export button
Do While appIE.Busy Or (appIE.READYSTATE <> 4) ' wait until IE has finished loading
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:07"))
Dim hwnd As LongPtr, h As LongPtr
Dim o As IUIAutomation ' The following steps are used to download a csv file from a webpage
Dim e As IUIAutomationElement
Set o = New CUIAutomation
Do While h = 0 'loops untill the frame notification window is avaliable, though THIS DOESN'T WORK FOR SOME REASON
h = appIE.hwnd ' handle of the current window
Set eBrowser = o.ElementFromHandle(ByVal h)
DoEvents
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString) ' replaces the handle of the window with the frame bar, using h in the function
Set eFNB = FindFromAllElementsWithClassName(eBrowser, "Frame Notification Bar", 10)
If eFNB Is Nothing Then
h = 0
End If
Loop
' we must find the first frame notification handle
If h = 0 Then Exit Sub
Set e = o.ElementFromHandle(ByVal h) ' it was important to set h as a LongPtr, and using Option Explicit at the top
Dim iCnd As IUIAutomationCondition
Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save") ' click the save button
Dim Button As IUIAutomationElement
Set Button = e.FindFirst(TreeScope_Subtree, iCnd) ' finds the button we need to click in the subtree on the notification bar
Dim InvokePattern As IUIAutomationInvokePattern
Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId) 'clicks the button
InvokePattern.Invoke
Application.Wait (Now + TimeValue("0:00:07"))
h = appIE.hwnd
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString) ' repeating the above for the second stage of open/save on the notification bar
If h = 0 Then Exit Sub
Set e = o.ElementFromHandle(ByVal h)
Dim iCnd2 As IUIAutomationCondition
Set iCnd2 = o.CreatePropertyCondition(UIA_NamePropertyId, "Open") ' similar to the above snippet, except for the second stage of the frame notification window
Dim Button2 As IUIAutomationElement
Set Button2 = e.FindFirst(TreeScope_Subtree, iCnd2)
Dim InvokePattern2 As IUIAutomationInvokePattern
Set InvokePattern2 = Button2.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern2.Invoke
Application.Wait (Now + TimeValue("0:00:03"))
结束子
我已经包含了 sub 中的所有代码,因为我觉得它为我正在采取的步骤提供了很好的上下文。这个似乎不起作用的片段是:
Do While h = 0 'loops untill the frame notification window is avaliable, though THIS DOESN'T WORK FOR SOME REASON
h = appIE.hwnd ' handle of the current window
Set eBrowser = o.ElementFromHandle(ByVal h)
DoEvents
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString) ' replaces the handle of the window with the frame bar, using h in the function
Set eFNB = FindFromAllElementsWithClassName(eBrowser, "Frame Notification Bar", 10)
If eFNB Is Nothing Then
h = 0
End If
Loop
谢谢,希望这很清楚。
基督教。