Excel 在VBA中标识UI自动化中的元素

Excel 在VBA中标识UI自动化中的元素,excel,vba,ui-automation,Excel,Vba,Ui Automation,我正在尝试学习VBA的一些新知识,现在我正在从事UI自动化。 有一个浏览器,我正试图以特定的方式与之交互,在遇到一个没有AutomationID、ClassName或Name的元素之前,一切都正常 如何识别该元素 快照取自“Windows辅助功能洞察”工具 这是我至今使用的代码 Rem References: UIAutomationClient Rem ------------------------------ Dim MyElement As UIAutomationClient.IUI

我正在尝试学习VBA的一些新知识,现在我正在从事UI自动化。 有一个浏览器,我正试图以特定的方式与之交互,在遇到一个没有AutomationID、ClassName或Name的元素之前,一切都正常 如何识别该元素

快照取自“Windows辅助功能洞察”工具

这是我至今使用的代码

Rem References: UIAutomationClient
Rem ------------------------------
Dim MyElement As UIAutomationClient.IUIAutomationElement
Dim MyElement1 As UIAutomationClient.IUIAutomationElement

'Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Enum oConditions
eUIA_NamePropertyId
eUIA_AutomationIdPropertyId
eUIA_ClassNamePropertyId
eUIA_LocalizedControlTypePropertyId
End Enum

Sub myDemo()
Dim AppObj As UIAutomationClient.IUIAutomationElement
Dim oInvokePattern As UIAutomationClient.IUIAutomationInvokePattern
Dim oAutomation As New CUIAutomation ' the UI Automation API\
Dim oPattern As UIAutomationClient.IUIAutomationLegacyIAccessiblePattern

Set AppObj = WalkEnabledElements("Y_browser")

Set MyElement = AppObj.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_AutomationIdPropertyId, "1"))
Set MyElement1 = MyElement.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_ClassNamePropertyId, "Edit"))
Set oPattern = MyElement1.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)
oPattern.SetValue ("http://student.moe.gov.eg/new/serch_students.aspx")
Set MyElement = AppObj.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_NamePropertyId, "GO"))
Set oInvokePattern = MyElement.GetCurrentPattern(UIAutomationClient.UIA_InvokePatternId)
oInvokePattern.Invoke

Set MyElement1 = AppObj.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_ClassNamePropertyId, "Shell Embedding"))
Set MyElement = MyElement1.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_ClassNamePropertyId, "Shell DocObject View"))
Set MyElement1 = MyElement.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_ClassNamePropertyId, "Internet Explorer_Server"))
Set MyElement = MyElement1.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_NamePropertyId, "الادارة الالكترونية للتعليم ...بيانات التلميذ"))
Set MyElement1 = MyElement.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_AutomationIdPropertyId, "100"))

Rem Here's where I am stuck at ....
Set MyElement = MyElement1.FindFirst(TreeScope_Children, PropCondition(oAutomation, eUIA_LocalizedControlTypePropertyId, "item"))

Stop

End Sub


Function PropCondition(UiAutomation As CUIAutomation, Prop As oConditions, Requirement As String) As UIAutomationClient.IUIAutomationCondition
Select Case Prop
    Case 0
        Set PropCondition = UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_NamePropertyId, Requirement)
    Case 1
        Set PropCondition = UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_AutomationIdPropertyId, Requirement)
    Case 2
        Set PropCondition = UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_ClassNamePropertyId, Requirement)
    Case 3
        Set PropCondition = UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_LocalizedControlTypePropertyId, Requirement)
End Select
End Function

Function WalkEnabledElements(strWindowName As String) As UIAutomationClient.IUIAutomationElement
Dim oAutomation As New CUIAutomation
Dim condition1 As UIAutomationClient.IUIAutomationCondition
Dim condition2 As UIAutomationClient.IUIAutomationCondition
Dim walker As UIAutomationClient.IUIAutomationTreeWalker
Dim element As UIAutomationClient.IUIAutomationElement

Set walker = oAutomation.ControlViewWalker
Set element = walker.GetFirstChildElement(oAutomation.GetRootElement)

Do While Not element Is Nothing
    Rem 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

Function GetElement(elementalist As UIAutomationClient.IUIAutomationElement)
On Error Resume Next
Dim oAutomation As New CUIAutomation
Dim walker As UIAutomationClient.IUIAutomationTreeWalker

Set walker = oAutomation.ControlViewWalker
Dim element1 As UIAutomationClient.IUIAutomationElementArray
Dim element2 As UIAutomationClient.IUIAutomationElement

Dim childtree As UIAutomationClient.TreeScope
Debug.Print elementalist.CurrentName
Dim condition1 As UIAutomationClient.IUIAutomationCondition
Set condition1 = oAutomation.CreateTrueCondition
Set element1 = elementalist.FindAll(TreeScope_Children, condition1)
DoEvents
If element1.Length <> 0 Then
    Set element2 = elementalist.FindFirst(TreeScope_Children, condition1)
End If

Do While Not element2 Is Nothing
    Dim oPattern As UIAutomationClient.IUIAutomationLegacyIAccessiblePattern
    Set oPattern = element2.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)

    Debug.Print element2.CurrentName & "|" & oPattern.CurrentValue

    If oPattern.CurrentName = "Notification" Then
        Set MyElement = element2
        Exit Function
    End If

    Debug.Print element2.CurrentClassName

    Debug.Print element2.CurrentAutomationId

    GetElement element2
    Debug.Print element2.CurrentName
    If Not MyElement Is Nothing Then Exit Function
    Set element2 = walker.GetNextSiblingElement(element2)
Loop

End Function

Function GetElement1(elementalist As UIAutomationClient.IUIAutomationElement)
On Error Resume Next
Dim oAutomation As New CUIAutomation
Dim walker As UIAutomationClient.IUIAutomationTreeWalker

Set walker = oAutomation.ControlViewWalker
Dim element1 As UIAutomationClient.IUIAutomationElementArray
Dim element2 As UIAutomationClient.IUIAutomationElement

Dim childtree As UIAutomationClient.TreeScope
Debug.Print elementalist.CurrentName
Dim condition1 As UIAutomationClient.IUIAutomationCondition
Set condition1 = oAutomation.CreateTrueCondition
Set element1 = elementalist.FindAll(TreeScope_Children, condition1)
DoEvents
If element1.Length <> 0 Then
    Set element2 = elementalist.FindFirst(TreeScope_Children, condition1)
End If

Do While Not element2 Is Nothing
    Dim oPattern As UIAutomationClient.IUIAutomationLegacyIAccessiblePattern
    Set oPattern = element2.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)

    Debug.Print element2.CurrentName & "|" & oPattern.CurrentValue

    If element2.CurrentName = "Save" Then
        Set MyElement = element2
        Exit Function
    End If

    Debug.Print element2.CurrentClassName

    Debug.Print element2.CurrentAutomationId

    GetElement element2
    Debug.Print element2.CurrentName
    If Not MyElement Is Nothing Then Exit Function
    Set element2 = walker.GetNextSiblingElement(element2)
Loop
End Function

Function AddReference() As Boolean
Dim VBAEditor As VBIDE.VBE
Dim vbProj As VBIDE.VBProject
Dim chkRef As VBIDE.Reference
Set VBAEditor = Application.VBE
Set vbProj = ThisWorkbook.VBProject

For Each chkRef In vbProj.References
    If chkRef.Name Like "*IBM PCOMM 4.01*" Then
        GoTo Flush
    End If
Next
On Error GoTo Hell:
vbProj.References.AddFromFile Environ("systemroot") & "\system32\uiautomationcore.dll"

Hell:
If Err.Number = 48 Then
    AddReference = False
ElseIf Err.Number = 0 Then
    AddReference = True
End If
Flush:
Set vbProj = Nothing
Set VBAEditor = Nothing
End Function
Rem引用:UIAutomationClient
雷姆------------------------------
将MyElement设置为UIAutomationClient.IUIAutomationElement
Dim MyElement1作为UIAutomationClient.IUIAutomationElement
'Private Declare Sub Sleep Lib“kernel32”(ByVal的长度为毫秒)
公共枚举条件
eUIA_NamePropertyId
eUIA_自动属性ID
eUIA_ClassNamePropertyId
eUIA_本地化控制类型属性ID
结束枚举
Sub myDemo()
作为UIAutomationClient.IUIAutomationElement的Dim AppObj
将oInvokePattern设置为UIAutomationClient.IUIAutomationInvokePattern
Dim oAutomation作为UI自动化API的新CUI自动化\
Dim oPattern作为UIAutomationClient.IUIAutomationLegacyAccessiblePattern
Set AppObj=walkabledelements(“Y_浏览器”)
设置MyElement=AppObj.FindFirst(TreeScope\u子对象,PropCondition(oAutomation,eUIA\u AutomationIdPropertyId,“1”))
Set MyElement1=MyElement.FindFirst(TreeScope\u子对象,PropCondition(oAutomation,eUIA\u ClassNamePropertyId,“编辑”))
Set oPattern=MyElement1.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)
oPattern.SetValue(“http://student.moe.gov.eg/new/serch_students.aspx")
Set MyElement=AppObj.FindFirst(TreeScope_Children,PropCondition(oAutomation,eUIA_NamePropertyId,“GO”))
设置oInvokePattern=MyElement.GetCurrentPattern(UIAutomationClient.UIA\u InvokePatternId)
oInvokePattern.Invoke
Set MyElement1=AppObj.FindFirst(TreeScope_子对象,PropCondition(oAutomation,eUIA_ClassNamePropertyId,“Shell嵌入”))
Set MyElement=MyElement1.FindFirst(TreeScope\u子对象,PropCondition(oAutomation,eUIA\u ClassNamePropertyId,“Shell文档对象视图”))
Set MyElement1=MyElement.FindFirst(TreeScope\u子对象,PropCondition(oAutomation,eUIA\u ClassNamePropertyId,“Internet Explorer\u服务器”))
设置MyElement=MyElement1.首先查找(树镜、儿童、专有条件(oAutomation、eUIA_NamePropertyId、Ou157a157;
Set MyElement1=MyElement.FindFirst(TreeScope\u子对象,PropCondition(oAutomation,eUIA\u AutomationIdPropertyId,“100”))
雷,这就是我被困的地方。。。。
Set MyElement=MyElement1.FindFirst(TreeScope\u子对象,PropCondition(oAutomation,eUIA\u LocalizedControlTypePropertyId,“项”))
停止
端接头
函数PropCondition(UiAutomation作为CUIAutomation,Prop作为条件,需求作为字符串)作为UIAutomationClient.IUIAutomationCondition
选择案例道具
案例0
Set-PropCondition=UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_-NamePropertyId,需求)
案例1
Set-PropCondition=UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA\U AutomationPropertyID,要求)
案例2
Set-PropCondition=UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA_ClassNamePropertyId,需求)
案例3
Set-PropCondition=UiAutomation.CreatePropertyCondition(UIAutomationClient.UIA\u LocalizedControlTypePropertyId,要求)
结束选择
端函数
作为UIAutomationClient.IUIAutomationeElement的函数WalkEnabledElements(strWindowName作为字符串)
Dim oAutomation作为一种新的自动化
Dim条件1作为UIAutomationClient.IUIAutomationCondition
Dim条件2作为UIAutomationClient.IUIAutomationCondition
Dim walker作为UIAutomationClient.IUIAutomationTreeWalker
作为UIAutomationClient.IUIAutomationElement的Dim元素
设置walker=oAutomation.ControlViewWalker
Set元素=walker.GetFirstChildElement(oAutomation.GetRootElement)
做而不做元素什么都不是
Rem Debug.Print element.CurrentName
如果InStr(1,element.CurrentName,strWindowName)>0,则
设置WalkEnabledElements=element
退出功能
如果结束
Set element=walker.GetNextSiblingElement(元素)
环
端函数
函数GetElement(ElementList作为UIAutomationClient.IUIAutomationeElement)
出错时继续下一步
Dim oAutomation作为一种新的自动化
Dim walker作为UIAutomationClient.IUIAutomationTreeWalker
设置walker=oAutomation.ControlViewWalker
作为UIAutomationClient.IUIAutomationElementArray的Dim元素1
作为UIAutomationClient.IUIAutomationElement的Dim元素2
Dim childtree作为UIAutomationClient.TreeScope
Debug.Print elementalist.CurrentName
Dim条件1作为UIAutomationClient.IUIAutomationCondition
Set condition1=oAutomation.CreateTrueCondition
Set element1=elementList.FindAll(TreeScope_子对象,条件1)
多芬特
如果元素1.长度为0,则
Set element2=elementList.FindFirst(TreeScope_子项,条件1)
如果结束
不做就去做元素2什么都不是
Dim oPattern作为UIAutomationClient.IUIAutomationLegacyAccessiblePattern
Set oPattern=element2.GetCurrentPattern(UIA_LegacyIAccessiblePatternId)
Debug.Print element2.CurrentName&“|”和oPattern.CurrentValue
如果oPattern.CurrentName=“通知”,则
设置MyElement=element2
退出功能
如果结束
Debug.Print element2.CurrentClassName
调试.打印元素2.CurrentAutomationId
GetElement元素2
调试.打印元素2.CurrentName
如果不是MyElement,则退出函数
Set element2=walker.GetNextSiblingElement(element2)
环
端函数
函数GetElement1(ElementList作为UIAutomationClient.IUIAutomationeElement)
出错时继续下一步
Dim oAutomation作为一种新的自动化
Dim walker作为UIAutomationClient.IUIAutomationTreeWalker
设置walker=oAutomation.ControlViewWalker
作为UIAutomationClient.IUIAutomationElementArray的Dim元素1
作为UIAutomationClient.IUIAutomationElement的Dim元素2
Dim childtree作为UIAutomationClient.TreeScope
Debug.Print elementalist.CurrentName
作为UIAutomationClient.I的Dim条件1