首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UIAutomation找不到子节点

UIAutomation找不到子节点
EN

Stack Overflow用户
提问于 2022-04-28 13:54:47
回答 1查看 252关注 0票数 0

为了使IIS (INetMgr)自动化,试图使用UIAutomation,我正在修复混合结果。如果屏幕元素很好的话,我就可以得到一些,其他的,甚至是直接的子节点,都不能用FindFirst_All或者在树型控件中尝试(内容\-只是不能得到所需的节点。有什么建议用来驱动UI来实现它的自动化吗?

窗口10/11桌面环境。

EN

回答 1

Stack Overflow用户

发布于 2022-04-29 06:48:01

下面是一个C#控制台应用程序,它从"Connections“窗格中转储InetMgr的项(最多2级)。

这必须作为管理员启动,否则它将失败(不是立即)。一般来说,UIA客户端必须在与自动化应用程序相同的UAC级别上运行。

为了确定从树中得到什么,或者是否可以做一些事情,在编写任何代码之前,我们可以使用从Windows检查工具或最近的无障碍洞察

另外,我使用的是Windows的UIAutomationClient COM对象,而不是Windows时代的旧对象,因为它错过了很多东西。

代码递归地迭代所有树项,如果它们没有使用ExpandCollapse控制模式展开,则展开它们,因为InetMgr的树具有延迟加载行为,因为它可能包含几十万项(在某些点映射到磁盘文件夹)。

代码语言:javascript
运行
复制
class Program
{
    // add a COM reference to UIAutomationClient (don't use .NET legacy UIA .dll)
    private static readonly CUIAutomation8 _automation = new CUIAutomation8(); // using UIAutomationClient;

    static void Main()
    {
        var process = Process.GetProcessesByName("InetMgr").FirstOrDefault();
        if (process == null)
        {
            Console.WriteLine("InetMgr not started.");
            return;
        }

        var inetmgr = _automation.ElementFromHandle(process.MainWindowHandle);
        if (inetmgr == null)
            return;

        // note: set "Embed Interop Type" to false in UIAutomationClient reference node or redefine all UIA_* used constants manually
        // also: you *must* this program as administrator to see this panel
        var connections = inetmgr.FindFirst(TreeScope.TreeScope_Subtree,
            _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, "_hierarchyPanel"));
        if (connections == null)
            return;

        var treeRoot = connections.FindFirst(TreeScope.TreeScope_Subtree,
            _automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_TreeItemControlTypeId));

        Dump(0, treeRoot);
    }

    static void Dump(int indent, IUIAutomationElement element)
    {
        var s = new string(' ', indent);
        Console.WriteLine(s + "name: " + element.CurrentName);

        // get expand/collapse pattern. All treeitem support that
        var expandPattern = (IUIAutomationExpandCollapsePattern)element.GetCurrentPattern(UIA_PatternIds.UIA_ExpandCollapsePatternId);
        if (expandPattern != null && expandPattern.CurrentExpandCollapseState != ExpandCollapseState.ExpandCollapseState_Expanded)
        {
            try
            {
                expandPattern.Expand();
            }
            catch
            {
                // cannot be expanded
            }
        }

        // tree can be huge,only dump 2 levels max
        if (indent > 2)
            return;

        var children = element.FindAll(TreeScope.TreeScope_Children, _automation.CreateTrueCondition());
        for (var i = 0; i < children.Length; i++)
        {
            Dump(indent + 1, children.GetElement(i));
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72044963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档