出现弹出窗口时如何单击保存按钮? [英] How to click on the save button when pop-up appears?

查看:77
本文介绍了出现弹出窗口时如何单击保存按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Internet Explorer单击文件.

I am using Internet Explorer to click on a file.

我到达一个出现Internet Explorer弹出窗口的位置,提示您要打开还是保存文件?":

I get to a point where an Internet Explorer pop-up appears saying "Do you want to open or save the file?":

我想编写一个VBA代码,单击保存"按钮.

I want to write a VBA code that clicks on the save button.

我意识到,右键单击并检查元素"以显示HTML页面是不可能的,因为弹出窗口不是Internet Explorer网页的一部分.

I realized it is not possible to right click and "inspect element" in order to show the HTML page because the pop-up is not part of the internet explorer webpage.

因此,即使它不可靠,我也尝试了sendKeys方法.我尝试了其他选项,例如:

So I tried the sendKeys method even though it is not reliable. I tried different options such as :

Application.SendKeys "%S"
Application.SendKeys "%s"
Application.SendKeys "%{S}"
Application.SendKeys "%{s}"
SendKeys ("%S")
SendKeys ("%s")
SendKeys ("%{S}")
SendKeys ("%{s}")

Application.SendKeys "%{S}"

我运行代码时,没有一个保存文件.

When I run the code, none of them save the file.

我的错误在哪里?

还有其他建议可以点击该保存"按钮吗?

Are there other propositions to click on that "Save" button?

也许我要向其应用SendKeys的对象不应该是"Application"?

Maybe the object to which I am applying SendKeys should not be "Application"?

推荐答案

如果您希望使用UIAutomationCore.dll并引用它,则可以执行以下操作:

If you wish to use the UIAutomationCore.dll and reference it, you can do something like:

Public Function AutoSave() As Boolean
On Error Goto handler
    Dim sysAuto As New UIAutomationClient.CUIAutomation
    Dim ieWindow As UIAutomationClient.IUIAutomationElement
    Dim cond As IUIAutomationCondition
    Set cond = sysAuto.CreateAndCondition(sysAuto.CreatePropertyCondition(UIA_NamePropertyId, "Notification"), _
                                      sysAuto.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ToolBarControlTypeId))
    Set ieWindow = sysAuto.GetRootElement.FindFirst(TreeScope_Descendants, cond)

    Dim tField As UIAutomationClient.IUIAutomationElement
    Dim tFieldCond As IUIAutomationCondition
    Set tFieldCond = sysAuto.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_SplitButtonControlTypeId)
    Set tField = ieWindow.FindFirst(TreeScope_Descendants, tFieldCond)

    Dim invPattern As UIAutomationClient.IUIAutomationInvokePattern
    Set invPattern = tField.GetCurrentPattern(UIA_InvokePatternId)
    invPattern.Invoke  
    AutoSave = True 

Exit Function
handler:

End Function

然后在单击该项目后调用该例程-可能要给它一个艰苦的等待,以允许显示通知栏.

And call that routine after clicking on the item - perhaps give it a Hard Wait to allow the Notification bar to show.

编辑

调用关闭按钮:

Set cond = sysAuto.CreateAndCondition(sysAuto.CreatePropertyCondition(UIA_NamePropertyId, "Close"), _
                                      sysAuto.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ButtonControlTypeId))
Dim closeButton As IUIAutomationElement
Set closeButton = WaitForElement(ieWindow, cond, 10)
If closeButton Is Nothing Then Exit Sub

Dim clickButtonPattern As IUIAutomationInvokePattern
Set clickButtonPattern = closeButton.GetCurrentPattern(UIA_InvokePatternId)
clickButtonPattern.Invoke
........

辅助功能:

Function WaitForElement(rootElement As IUIAutomationElement, condition As IUIAutomationCondition, timeout As Long) As IUIAutomationElement
    Dim startTime As Date
    startTime = Now

    Dim element As IUIAutomationElement
    Set element = rootElement.FindFirst(TreeScope_Descendants, condition)
    While element Is Nothing And 100000 * (Now - startTime) < timeout
        Application.Wait Now + TimeValue("00:00:01")
        Set element = rootElement.FindFirst(TreeScope_Descendants, condition)
    Wend

    Set WaitForElement = element
End Function

您通常会等到通知栏文本"元素文本更改为末尾下载已完成".

You would typically wait until the 'Notification bar Text' element text had changed to have "download has completed" at the end.

这可以通过几个辅助函数来完成:

This can be done with a couple of helper functions:

Function WaitForTextValue(textElement As IUIAutomationElement, text As String, timeout As Long, Optional exactMatch As Boolean = False) As Boolean
Dim startTime As Date
startTime = Now

Dim result As String
result = ReadValue(textElement)
Dim isMatch As Boolean
If exactMatch Then
    isMatch = result = text
Else
    isMatch = InStr(1, result, text, vbTextCompare) > 0
End If

'keep reading the element until we have a match, or the timeout has expired
While Not isMatch And 100000 * (Now - startTime) < timeout
    Application.Wait Now + TimeValue("00:00:01")
    result = ReadValue(textElement)
    If exactMatch Then
        isMatch = result = text
    Else
        isMatch = InStr(1, result, text, vbTextCompare) > 0
    End If
Wend

WaitForTextValue = isMatch
End Function

Function ReadValue(element As IUIAutomationElement) As String
Dim valPattern As IUIAutomationValuePattern
Set valPattern = element.GetCurrentPattern(UIA_ValuePatternId)

If Not valPattern Is Nothing Then
    ReadValue = element.GetCurrentPropertyValue(UIA_ValueValuePropertyId)
Else
    ' raise error here if element's value cannot be read - err.Raise
End If

End Function

您将在单击保存"按钮后调用此检查,如下所示:

And you would call this check after clicking the 'Save' button like so:

Set cond = sysAuto.CreateAndCondition(sysAuto.CreatePropertyCondition(UIA_NamePropertyId, "Notification bar Text"), _
                                      sysAuto.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_TextControlTypeId))
Dim barText As IUIAutomationElement
Set barText = WaitForElement(ieWindow, cond, 10)
If barText Is Nothing Then Exit Sub

If Not WaitForTextValue(barText, "download has completed", 30, False) Then Exit Sub
'if we get to here, then text has changed, and we can go ahead and click close button

这篇关于出现弹出窗口时如何单击保存按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆