早上好,我正在努力寻找有关互联网上似乎没有太多可用信息的问题的信息-即 Internet Explorer 中的“框架通知栏”(询问您是否要“保存”的黄色小窗口或“打开”下载的文件)。
我将切入正题,我遇到的问题是我的代码在 Internet Explorer 可见性设置为 true 时工作,但在可见性设置为 false 时不起作用。我已经在这两种情况下逐步查看了代码以查看发生了什么变化,并注意到框架通知栏的句柄更改了值,但除此之外都是相同的。相关代码为:
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Sub StartIE()
Dim appIE As Object
Dim URLString As String
Dim HTMLdoc, btn As Object
Set appIE = CreateObject("internetexplorer.application") ' create an instance of internet explorer
With appIE
.Navigate "https://analytics.twitter.com/user" 'this url wont work for you. you will need to have your own twitter account on twitter analytics, and copy the link to the "tweets" page
.Visible = True ' and show the IE
End With
Do While appIE.Busy Or (appIE.READYSTATE <> 4) ' wait until IE has finished loading
DoEvents
Loop
URLString = appIE.LocationURL
Set HTMLdoc = appIE.document
Set btn = HTMLdoc.getElementsByClassName("btn btn-default ladda-button")(0) 'finds the export data button
btn.Click
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
h = appIE.hwnd
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString) ' we must find the first frame notification handle
If h = 0 Then Exit Sub
Set e = o.ElementFromHandle(ByVal h)
Dim iCnd As IUIAutomationCondition
Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save")
Dim Button As IUIAutomationElement
Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
Dim InvokePattern As IUIAutomationInvokePattern
Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern.Invoke
h = appIE.hwnd
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
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
End Sub
在这段代码中,我认为出现问题的代码段位于:
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
h = appIE.hwnd
h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString) ' we must find the first frame notification handle
If h = 0 Then Exit Sub
Set e = o.ElementFromHandle(ByVal h)
Dim iCnd As IUIAutomationCondition
Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save")
Dim Button As IUIAutomationElement
Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
Dim InvokePattern As IUIAutomationInvokePattern
Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern.Invoke
谁能告诉我为什么会发生这种情况,以及如何解决它?我知道我已经问了很多,但我真的很喜欢任何修复的解释,因为我正在努力提高我的理解,并发现它对处于类似情况的其他人有益:)
先感谢您。