【问题标题】:How To Press Save on Internet Explorer Popup?如何在 Internet Explorer 弹出窗口上按保存?
【发布时间】:2020-10-13 20:13:02
【问题描述】:

如何操作下面的 Internet Explorer 下载弹出窗口。

我尝试了下面的代码,但这不是框架通知栏,所以没有帮助。

Sub PressSave(ie As InternetExplorer)
    Dim o As IUIAutomation
    Dim e As IUIAutomationElement
    Application.Wait (Now + TimeSerial(0, 0, 4))
    Set o = New CUIAutomation
    Dim h As Long
    h = ie.hwnd
    'find notification bar
    h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
    If h = 0 Then Exit Sub
    
    'find save button
    Set e = o.ElementFromHandle(ByVal h)
    Dim iCnd As IUIAutomationCondition
    Dim Button As IUIAutomationElement
    Do Until Not Button Is Nothing
        Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save")
        Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
    Loop
    
    'click save
    Dim InvokePattern As IUIAutomationInvokePattern
    Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
    InvokePattern.Invoke
    
    'wait till file is completely downloaded
    Dim iOpen As IUIAutomationCondition
    Dim OpenButton As IUIAutomationElement
    Do Until Not OpenButton Is Nothing
        Set iOpen = o.CreatePropertyCondition(UIA_NamePropertyId, "View 
downloads")
        Set OpenButton = e.FindFirst(TreeScope_Subtree, iOpen)
    Loop
End Sub

我尝试使用 API 来获取数据,但一直收到错误 401,这意味着它需要身份验证,我不确定是否可以添加参数来提供它。

Sub APIToGetData()
Dim sURL As String
Dim savePath As String
Dim WinHttpReq As Object

savePath = "C:\Downloads"
sURL = "https://[myurlhere]/api/admin/data_dumps/download"
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")

WinHttpReq.Open "GET", sURL, False
WinHttpReq.send

If WinHttpReq.Status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.responseBody
    oStream.Close
End If
End Sub

如何在不使用 SendKeys 的情况下操作此弹出窗口?

【问题讨论】:

  • 您能否通过使用 API 下载文件来解决此问题?
  • 更新了我的问题。不确定它是否有区别,但它是一组文件
  • 如果您要走 API 路线,您需要查看文档。通常,您首先发出一个 Web 请求,该请求通过 API 进行身份验证,基本上您会返回一个字符串,该字符串将您标识为授权用户。通常你有用户名/密码或 API 密钥之类的东西。一旦通过身份验证,您将身份验证信息(通常在标头中)传递到对服务的后续调用中。
  • 已检查,但我认为我无法使用 API。每次都会生成一个新密钥。
  • 根据实施情况,这可能是预期的。你能分享你正在审查的 API 吗?

标签: excel vba


【解决方案1】:

大约一年前,我遇到了类似的问题,我可以通过实现 API 来解决

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal Msg As Integer, ByVal wParam As Long, ByVal lParam As Long) As Long

使用 FindWindow API 我实现了一种监视程序

Do Until FindWindow(vbNullString, "Internet Explorer") > 0
    Sleep 100
    DoEvents
Loop

找到窗口后,我让它确认默认选择(在我的例子中是“打开”),直到窗口消失

Do Until FindWindow(vbNullString, "Internet Explorer") = 0
    SendMessage FindWindow(vbNullString, "Internet Explorer"), WM_COMMAND, IDOK, 0
    Sleep 100
    DoEvents
Loop

SendMessage 很棒,因为它将命令直接发送到目标 hWnd,而 SendKeys 只是将指定的击键发送到焦点所在的任何位置。

【讨论】:

  • 当我 degub.print FindWindow(vbNullString, "Internet Explorer"),它给出 "11143362"。所以它总是大于 0
  • 只有在标题为“Internet Explorer”的窗口打开时才应大于 0。
【解决方案2】:

Tate Garringer 的回答具有正确的逻辑,但由于某种原因对我不起作用。我最终找到了一些有用的东西,以及大量有用的信息: https://www.mrexcel.com/forum/excel-questions/502298-need-help-regarding-ie-automation-using-vba-3.html

以下是我问题的直接答案:

'Find the Save As window, waiting a maximum of 10 seconds for it to appear
timeout = Now + TimeValue("00:00:10")
Do
    hwnd = FindWindow("#32770", "Save As")
    DoEvents
    Sleep 300
Loop Until hwnd Or Now > timeout

希望这对其他人有帮助

【讨论】:

    猜你喜欢
    • 2012-01-25
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    相关资源
    最近更新 更多