【问题标题】:Only one child found for a window, although inspect.exe shows all children只为一个窗口找到一个孩子,尽管 inspect.exe 显示所有孩子
【发布时间】:2015-12-12 00:59:11
【问题描述】:

我在使用 C# UIAutomation 查找 Firefox 的 UI 元素时遇到问题。

在 Inspect.exe 中,一切正常:

但是,当以 root 身份在窗口上使用下面的方法时,只能找到以黄色突出显示的元素。我需要到达蓝色元素,但无法获得它。以下解决方案基于这篇文章:

UIAutomation won't retrieve children of an element

public static void WalkControlElements(AutomationElement rootElement)
    {
        if (rootElement == null) return;

        var children = new List<AutomationElement>();

        var currentChild = TreeWalker.RawViewWalker.GetFirstChild(rootElement);

        while (currentChild != null)
        {
            children.Add(currentChild);
            currentChild = TreeWalker.RawViewWalker.GetNextSibling(currentChild);
        }

        foreach (var child in children)
        {
            Console.WriteLine(child.ToString());
            WalkControlElements(child);
        }
    }

【问题讨论】:

    标签: c# ui-automation


    【解决方案1】:

    因为它们不是处理它们的窗口。它们是其根窗口的元素。您必须掌握元素(或项目)的句柄。

    【讨论】:

    • 好的,我该怎么做?
    • 我的任何项目都没有使用 UIAutomation。我在 C# 中使用“FindWindowEx”、“FindWindow”、“FindWindowByIndex”和“SendMessage”方法进行自动化。例如: Intptr WindowHandler = FindWindow("WindowsForms10.Window.8.app.0.33c0d9d", "FOREXTrader PRO - LIVE");在他之后我使用 FindWindowEx(WindowHandler,Intpr,Zero,QRibbonCaption1"); 然后我通过 FindWindowByIndex 获取用户名字段并发送带有我想要的用户名值的“WM_SETTEXT”消息类型。
    • 在 C# 中搜索 FindWindow 并获取 C# 中其他应用程序的句柄和挂钩
    • 请注意,对于 Firefox 等浏览器的自动化,最好使用扩展(插件)。通过 JavaScript,您可以获得所需的节点并非常容易地更改它们的值。您可以通过套接字编程或其他方法(如读取和写入文件)从您的插件与您的 C# 应用程序进行通信。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多