首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单击AutomationElement对话框

单击AutomationElement对话框
EN

Stack Overflow用户
提问于 2018-10-26 13:54:54
回答 1查看 642关注 0票数 1

我正在自动化一个安装程序,因为它没有(功能) /silent开关。它的工作基本上很好,但我遇到了一个似乎是路障-一个可怕的对话框。看起来像

我试过的是:

代码语言:javascript
复制
SendKey.Send/Wait(Enter/Escape)
PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK__RETURN, 0);

关键注入到过程中

代码语言:javascript
复制
injector.SimulateTap((int) centre.X + 150, (int) centre.Y + 75); 

触摸注入,不注册在这个特定的窗口,但所有其他窗口工作良好。

代码语言:javascript
复制
Walking over the element as if it were just a normal element

我几乎不知所措,因为我非常希望不用手动按这个按钮(这也会破坏其他的自动安装)。

我知道为什么找不到它(或者我认为是这样);因为它是一个新的对话,我认为enter不能工作,因为它被发送到错误的进程(对话框的父进程,安装程序)和我不知道的触摸注入。

文档没有讨论如何处理对话框(也都是C++)。

通常,我正在遍历元素,以找到继续安装的下一个控件。有几个控件:

代码语言:javascript
复制
    string[] EGalaxTouchInstallSequence = {
        @"Next >",
        "I accept the terms of the license agreement",
        @"Next >",
        "None",
        null, //this is supposed to represent a dialogue popup
        "OK",
        "Support Multi-Monitor System",
        @"Next >",
        @"Next >",
        @"Next >",
        @"Next >",
        @"Next >"
    };

最初,我从窗口获取所有的句柄,如下所示:

代码语言:javascript
复制
    AutomationElement UIElement = AutomationElement.FromHandle(SetupWindow);

我重复了一遍列表:

代码语言:javascript
复制
        foreach (string button in EGalaxTouchInstallSequence)
        {
            if (!FindAndClickElement(UIElement, button))
                goto error;
        }

FindAndClickElement

代码语言:javascript
复制
    private bool FindAndClickElement(AutomationElement UIElement, string ElementName)
    {
        AutomationElement element = null;

        Condition ControlProperty = new PropertyCondition(AutomationElement.NameProperty, ElementName);

        AutomationElementCollection elements = UIElement.FindAll(TreeScope.Descendants, ControlProperty);

        foreach (AutomationElement e in elements)
        {
            if (e.Current.Name.Contains(ElementName))
            {
                Console.WriteLine("Match with: " + ElementName);
                element = e;
            }

        }
        //Click code is further down, irrelevant at this stage
     }

Element Exists

代码语言:javascript
复制
private bool ElementExists(AutomationElement UIElement, string ElementName)
{
    Condition ControlProperty = new PropertyCondition(AutomationElement.NameProperty, ElementName);

    AutomationElementCollection elements = UIElement.FindAll(TreeScope.Descendants, ControlProperty);

    foreach (AutomationElement e in elements)
    {
        Console.WriteLine($"element name: {e.Current.Name}");
        if (e.Current.Name.Contains(ElementName))
        {
            return true;
        }
    }

    return false;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-26 15:12:29

在你得到元素之前,我会检查一下里面有什么。我用这样的方法来得到所有的东西。

代码语言:javascript
复制
  /// <summary>
  ///    Returns all children elements of an automation element.
  /// </summary>
  /// <param name="automationElement"></param>
  /// <returns></returns>
  public static List<AutomationElement> AllChildren(AutomationElement automationElement)
  {
     if (automationElement == null) throw new ArgumentNullException(nameof(automationElement));

     AutomationElement firstChild = TreeWalker.RawViewWalker.GetFirstChild(automationElement);
     if (firstChild == null)
        return new List<AutomationElement>();

     List<AutomationElement> children = new List<AutomationElement> { firstChild };

     AutomationElement sibling;
     int childIndex = 0;
     while ((sibling = TreeWalker.RawViewWalker.GetNextSibling(children[childIndex])) != null)
     {
        children.Add(sibling);
        childIndex++;
     }

     return children;
  }

此外,在我的评论中,请确保您不会意外地通过使用这个FindAll("...").FirstOrDefault(a => a.FirstChild().ClassName != "ToolTip");找到工具提示。工具提示通常不会显示,因为应用程序点击东西的速度非常快,所以它的位置将有一个(-1,-1)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53010335

复制
相关文章

相似问题

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