Я очень новичок в UI Automation, и я делаю небольшой проект по автоматизации настольной системы от VBA Exel.Я хочу писать, читать и нажимать на разные кнопки этого настольного приложения.Я использую dll UIAutomationClient.Опция чтения и записи работает хорошо, но когда я использую метод click во всем методе Invoke, у меня появляется следующее сообщение об ошибке: Переменная объекта или блок Не установлено.Я использую Inspect.exe для того, чтобы отследить дерево кнопки.Что я понял, эта кнопка находится внутри панели.Это код, который у меня есть до сих пор.Пожалуйста, прости меня за некоторые технические ошибки или знания, но я новичок в программировании.
findTheParentElement
requirementGroup = Array("TFormSeleccionPago1", "TPanel", "TAdvSmoothPanel", "Siguiente")
idTypeGroup = Array("ClsName", "ClsName", "ClsName", "Name")
valueFound = executeOperation("click", requirementGroup, idTypeGroup)
Function findTheParentElement() As UIAutomationClient.IUIAutomationElement
Set parentElement = WalkEnabledElements(oAutomation.GetRootElement, "softwareNameInDesktop")
End Function
Function executeOperation(ByVal operationType As String, ByVal requirementGroup As Variant, ByVal idTypeGroup As Variant, Optional valueToWrite As String) As String
Dim oInvokePattern As UIAutomationClient.IUIAutomationInvokePattern
Dim oPattern As UIAutomationClient.IUIAutomationLegacyIAccessiblePattern
Dim valueFound As String
Dim odd As Boolean
odd = False
If UBound(requirementGroup) >= 0 Then
Set MyElement1 = parentElement.FindFirst(TreeScope_Children, PropCondition(oAutomation, requirementGroup(0), idTypeGroup(0)))
odd = True
End If
If UBound(requirementGroup) >= 1 Then
Set MyElement2 = MyElement1.FindFirst(TreeScope_Children, PropCondition(oAutomation, requirementGroup(1), idTypeGroup(1)))
odd = False
End If
If UBound(requirementGroup) >= 2 Then
Set MyElement1 = MyElement2.FindFirst(TreeScope_Children, PropCondition(oAutomation, requirementGroup(2), idTypeGroup(2)))
odd = True
End If
If UBound(requirementGroup) >= 3 Then
Set MyElement2 = MyElement1.FindFirst(TreeScope_Children, PropCondition(oAutomation, requirementGroup(3), idTypeGroup(3)))
odd = False
End If
If UBound(requirementGroup) >= 4 Then
Set MyElement1 = MyElement2.FindFirst(TreeScope_Children, PropCondition(oAutomation, requirementGroup(4), idTypeGroup(4)))
odd = True
End If
If UBound(requirementGroup) >= 5 Then
Set MyElement2 = MyElement1.FindFirst(TreeScope_Children, PropCondition(oAutomation, requirementGroup(5), idTypeGroup(5)))
odd = False
End If
Select Case operationType
Case "write"
If odd = False Then
Set oPattern = MyElement2.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)
Else
Set oPattern = MyElement1.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)
End If
oPattern.SetValue (valueToWrite)
executeOperation = "write"
Case "read"
If odd = False Then
Set oPattern = MyElement2.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)
Else
Set oPattern = MyElement1.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)
End If
valueFound = oPattern.CurrentValue 'with this property I can retrieve the information
executeOperation = valueFound
Case "click"
If odd = False Then
Set oInvokePattern = MyElement2.GetCurrentPattern(UIAutomationClient.UIA_ValuePatternId)
Debug.Print MyElement2.CurrentAccessKey
Else
Set oInvokePattern = MyElement1.GetCurrentPattern(UIAutomationClient.UIA_ValuePatternId)
Debug.Print MyElement1.CurrentControlType
End If
oInvokePattern.Invoke
executeOperation = "click"
Case "grid"
Set oInvokePattern = MyElement1.GetCurrentPattern(UIAutomationClient.UIA_GridPatternId)
value = oGridPattern.getItem(1, 2)
executeOperation = "grid"
End Select
End Function
Function PropCondition(UiAutomation As CUIAutomation, ByVal Requirement As String, ByVal idType As String) As UIAutomationClient.IUIAutomationCondition
Select Case idType
Case "Name"
Set PropCondition = UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_NamePropertyId, Requirement)
Case "AutoID"
Set PropCondition = UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_AutomationIdPropertyId, Requirement)
Case "ClsName"
Set PropCondition = UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_ClassNamePropertyId, Requirement)
Case "LoczCon"
Set PropCondition = UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_LocalizedControlTypePropertyId, Requirement)
End Select
End Function
Function WalkEnabledElements(element As UIAutomationClient.IUIAutomationElement, strWindowName As String) As UIAutomationClient.IUIAutomationElement
Dim walker As UIAutomationClient.IUIAutomationTreeWalker
Set walker = oAutomation.ControlViewWalker
Set element = walker.GetFirstChildElement(element)
Do While Not element Is Nothing
Debug.Print element.CurrentName
If InStr(1, element.CurrentName, strWindowName) > 0 Then
Set WalkEnabledElements = element
Exit Function
End If
Set element = walker.GetNextSiblingElement(element)
Loop
End Function