c# - 在Windows Automation中使用部分NameProperty查找Windows控件

标签 c# windows ui-automation microsoft-ui-automation

我正在尝试使用部分NameProperty识别Windows静态文本控件。这是我的代码:

// Get a reference to the window
AutomationElement epoWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "MsiDialog"));
// Get a reference to the control
AutomationElement epoControl = epoWindow.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, controlText));

目前,我需要完整的controlText字符串才能正常工作,但我想搜索该字符串的一部分并返回找到的所有控件。
我该怎么做呢?
谢谢,
约翰

最佳答案

您可以使用预定义的TrueCondition对子集合进行迭代,如下所示:

  foreach(AutomationElement child in epoWindow.FindAll(TreeScope.Subtree, Condition.TrueCondition))
  {
      if (child.Current.Name.Contains("whatever"))
      {
          // do something
      }
  }

PS:如果您不想降低应用程序的性能(如果它具有较大的子层次结构)或无限期等待,则需要仔细选择TreeScope

关于c# - 在Windows Automation中使用部分NameProperty查找Windows控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30804184/

相关文章:

c# - UIPageViewController 的 DidFinishAnimating 和 WillTransition,如何正确实现这些方法?

c# - 从c#生成Word文档

Windows 批处理脚本 - 将文件名读入变量

python - 检查元素是否存在python selenium

python - 如何选择弹出消息 "Allow",它是 HTML 页面的 Span 元素。预计使用 find_element_by_css_selector 解决方案

c# - 如何使用 NSubstitute 假调用当前类的方法?

c# - 将右键单击菜单项添加到图片框

windows - 如何在不编写自己的程序的情况下将某些文件上传到 Azure Blob 存储?

python - Win7以上使用Python关闭Chrome浏览器

c# - 使用 UIAutomation 进行单元测试