对象引用未设置为对象的实例(CacheRequest)(UI)

时间:2014-01-16 11:39:20

标签: c# wpf winforms ui-automation

您好我正在使用此代码读取datagridview cell.Its工作正常

       var myString = targetText.Cached.Name;

targettext为null

代码:

       LogMessage("Getting RootElement...");
        AutomationElement rootElement = AutomationElement.RootElement;
        if (rootElement != null)
        {
            LogMessage("OK." + Environment.NewLine);

            Automation.Condition condition = new PropertyCondition(AutomationElement.NameProperty, "Form1");

            LogMessage("Searching for Test Window...");
            AutomationElement appElement = rootElement.FindFirst(TreeScope.Children, condition);

            if (appElement != null)
            {
                LogMessage("OK " + Environment.NewLine);
                LogMessage("Searching for Gridview control...");
                AutomationElement txtElementA = GetTextElement(appElement, "dg");
                var rows = txtElementA.FindAll(TreeScope.Children, PropertyCondition.TrueCondition);
                foreach (AutomationElement loginLine in rows)
                {
                    var loginLinesDetails = loginLine.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));

                    for (var i = 0; i < loginLinesDetails.Count; i++)
                    {
                        var cacheRequest = new CacheRequest
                        {
                            AutomationElementMode = AutomationElementMode.None,
                            TreeFilter = System.Windows.Automation.Automation.RawViewCondition
                        };

                        cacheRequest.Add(AutomationElement.NameProperty);
                        cacheRequest.Add(AutomationElement.AutomationIdProperty);

                        cacheRequest.Push();

                        var targetText = loginLinesDetails[i].FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "TextBlock"));// targettext is null

                        cacheRequest.Pop();

                        var myString = targetText.Cached.Name;//Object reference not set ///to instance of an object error
                    }

                }

            }
            else
            {
                WriteLogError();
            }
        }

这是我目前正在尝试阅读的示例表单的图像。如果它适用于这个小应用程序,它肯定适用于大型应用程序。

enter image description here

我想要做的就是阅读datagridview的单元格。我不知道我做得对不对,任何人都可以帮助我。 如果有人能帮助我,我们将非常感激

2 个答案:

答案 0 :(得分:3)

在尝试将值分配给

之前
var myString = targetText.Cached.Name;

检查以下条件

if(targetText != null && targetText.Cached!=null)
   var myString = targetText.Cached.Name;

下一行可能会返回NULL

loginLinesDetails[i].FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "TextBlock"));

答案 1 :(得分:1)

问题可能是您正在寻找TextBox控件,如行所示

LogMessage("Searching for TextBox A control...");

但在PropertyCondition中,您使用"TextBlock"作为班级名称。

然后你应该这样写:

var targetText = loginLinesDetails[i].FindFirst(TreeScope.Children,
    new PropertyCondition(AutomationElement.ClassNameProperty, "TextBox")); // here