【问题标题】:Get attributes from UI element with Microsoft UIAutomation and Python使用 Microsoft UIAutomation 和 Python 从 UI 元素获取属性
【发布时间】:2020-11-04 08:38:55
【问题描述】:

目标:我想检索 UI 元素的以下属性:名称、类、控件类型和父名称。该元素可能属于在 Internet Explorer 上打开的 Outlook 或 Web 应用程序。

Python 解释器: 3.7

Python 库: Comtypes

研究:

OUTLOOK: 为了在 Microsoft Outlook 中检索特定元素的属性,我使用 comtypes 创建 UIautomationCore.dll 的对象。我可以根据特定元素的点来检索名称和类,但我找不到检索控件类型的方法,另一方面是具有该点的父元素名称。我在 Microsoft UI 自动化中发现我必须使用 TreeWalker 才能获取有关父元素的信息。但是,我不知道如何在 Python 中实现。

WEB 应用程序(Internet Explorer):我使用的是同一个库,我可以检索位于 Internet Explorer 浏览器中的元素。但是,当我无法获取位于 Web 应用程序(正文)内容中的一个按钮的这些属性时。

问题:如何通过 Microsoft UI 自动化检索 Outlook 中的控件类型和父元素名称?以及在使用 Internet Explorer 时如何检索这些属性?

我当前的代码:

import comtypes
from comtypes.client import *

comtypes.client.GetModule('UIAutomationCore.dll')
from comtypes.gen.UIAutomationClient import *

# get IUIAutomation interface
uia = CreateObject(CUIAutomation._reg_clsid_, interface=IUIAutomation)


# import tagPOINT from wintypes
from ctypes.wintypes import tagPOINT
point = tagPOINT(1833, 95)
element = uia.ElementFromPoint(point)

walker = ViewWalker
parentElement = walker.GetParent(element)
name=element.currentName
parentName = parentElement.currentName

print("elementName:", name)
print("parentElementName: ", parentName)

赞成:有人可以就如何用 Python 实现 IUIAutomationElement COM 接口给出一些指导吗?任何链接或书籍都可能对理解它非常有用。

【问题讨论】:

    标签: python microsoft-ui-automation comtypes


    【解决方案1】:

    模块 pywinauto 具有这种功能(基本上包装/抽象了您想要执行的所有这些 COM 内容,以便您访问 UIAutomationElement 信息)。 对于基础知识: https://pywinauto.readthedocs.io/

    但是,如果您对如何使用 COM 接口与 python 完成的繁琐细节感兴趣,那么他们的代码和文档将是一个很好的起点!

    【讨论】:

    • 嗨@Julia Rozanova。感谢您的帮助和时间。据我了解,pywinauto 无法获得网站的正文。如果是这样,我该如何实现?
    【解决方案2】:

    有这方面的库:

    我最喜欢的,uiautomation。有检查员,图书馆很齐全。虽然它不能 100% 与 IE 和 Java 应用程序一起工作。

    https://pypi.org/project/uiautomation/

    有了这个,您可以通过将鼠标放在每个窗口元素上来获取它的选择器

    from uiautomation import uiautomation as auto
    
    while True:
        control = auto.ControlFromCursor()
        print("Name", control.Name)
        print("AutomationID", control.AutomationId)
    

    另一个库是pywinauto,但我用的不多,所以不能告诉你更多

    【讨论】:

      猜你喜欢
      • 2014-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 2017-09-24
      相关资源
      最近更新 更多