首页 > 解决方案 > UIElement 返回空文本

问题描述

我正在编写需要拦截AutomationElement应用程序的软件。这AutomationElement是在 a 中定义的ListBox。当我使用 inspect.exe 检查如何获取值时,各自AutomationElement没有子级。

这是我用来尝试获取的代码ListItem

AutomationElement desktop = AutomationElement.FromHandle (tskBarHwndTest);
AutomationElement dataGrid1 = desktop.FindFirst (System.Windows.Automation.TreeScope.Descendants, new PropertyCondition (AutomationElement.AutomationIdProperty, "QueueListView"));
if (dataGrid1! = null)
{
    AutomationElementCollection lines1 = dataGrid1.FindAll (System.Windows.Automation.TreeScope.Descendants, new PropertyCondition (AutomationElement.ControlTypeProperty, ControlType.ListItem));
    GridPattern pattern = GetGridPattern (dataGrid1);
    AutomationElement tempElement = pattern.GetItem (0, 2)
}

截图检查

标签: c#ui-automation

解决方案


我看到您正在寻找,"QueueListView"但在 inspect.exe 屏幕截图中,我没有看到类似AutomationElement桌面的后代AutomationId

我建议System.Windows.Automation.TreeScope.Children您一次使用TreeScope一个元素。如果您的应用程序不平凡,那么在使用Descendants时查找任何内容都会非常缓慢。

由于我看不到检查中的所有值,它看起来像这样。

AutomationElement desktop = AutomationElement.RootElement;
AutomationElment mainWindow = desktop.FindFirst(System.Windows.Automation.TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "<Your Main Window Name>");
//... add code here to get from main window to where your screen shot starts
AutomationElement panello1 = mainWindow.FindAll(System.Windows.Automation.TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "pannello"))[2];
AutomationElement tabulazione = panello1.FindFirst(System.Windows.Automation.TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "tabulazione"));
AutomationElement panello2 = tabulazione.FindFirst(System.Windows.Automation.TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "panello"));
AutomationElement interazioniPersonali = panello2.FindFirst(System.Windows.Automation.TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Interazioni personali"));
AutomationElement elenco = interazioniPersonali.FindFirst(System.Windows.Automation.TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "elenco"));
AutomationElement voceDiElenco = interazioniPersonali.FindFirst(System.Windows.Automation.TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "voce di elenco"));
AutomationElement numero = voceDiElenco.FindAll(System.Windows.Automation.TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "testo"))[2];
//... in if you expand the selected AutomationElement in your screenshot there should be a text element that contains the text you want to get

这绝对可以改进,但这只是基于我在屏幕截图中看到的。


推荐阅读