C# “错误”;值可以';“不能为空”;,自动化元件

C# “错误”;值可以';“不能为空”;,自动化元件,c#,google-chrome,ui-automation,C#,Google Chrome,Ui Automation,因此,我试图使用C#中的UIAutomation从Chrome获取所有打开的选项卡,但我不断得到错误: 发生System.ArgumentNull异常 HResult=0x80004003 Message=值不能为空。 Source=UIAutomationClient StackTrace: 位于System.Windows.Automation.TreeWalker.GetParent(AutomationElement元素) 在C:\Users…\chrometabtest\chromet

因此,我试图使用C#中的UIAutomation从Chrome获取所有打开的选项卡,但我不断得到错误:

发生System.ArgumentNull异常

HResult=0x80004003
Message=值不能为空。
Source=UIAutomationClient

StackTrace:
位于System.Windows.Automation.TreeWalker.GetParent(AutomationElement元素)
在C:\Users…\chrometabtest\chrometabtest\Program.cs中的chrometabtest.Program.Main(字符串[]args)处:第31行

该错误在代码中用注释表示

    using System;
using System.Diagnostics;
using System.Windows.Automation;

namespace chromeTabsTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Process[] procsChrome = Process.GetProcessesByName("chrome");
            if (procsChrome.Length <= 0)
            {
                Console.WriteLine("Chrome is not running");
            }
            else
            {
                foreach (Process proc in procsChrome)
                {
                    // the chrome process must have a window 
                    if (proc.MainWindowHandle == IntPtr.Zero)
                    {
                        continue;
                    }
                    // to find the tabs we first need to locate something reliable - the 'New Tab' button 
                    AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
                    Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "New Tab");
                    AutomationElement elmNewTab = root.FindFirst(TreeScope.Descendants, condNewTab);
                    // get the tabstrip by getting the parent of the 'new tab' button 
                    TreeWalker treewalker = TreeWalker.ControlViewWalker;
                    AutomationElement elmTabStrip = treewalker.GetParent(elmNewTab); // <------------- Error here 
                    // loop through all the tabs and get the names which is the page title 
                    Condition condTabItem = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
                    foreach (AutomationElement tabitem in elmTabStrip.FindAll(TreeScope.Children, condTabItem))
                    {
                        Console.WriteLine(tabitem.Current.Name);
                    }
                }
            }


            Console.Write("\nPress any key to continue...");
            Console.ReadKey();
        }
    }
}
使用系统;
使用系统诊断;
使用System.Windows.Automation;
名称空间色度测试
{
班级计划
{
静态void Main(字符串[]参数)
{
Process[]procsChrome=Process.getProcessByName(“chrome”);

如果(procsChrome.Length以下行似乎对语言敏感:

Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "New Tab");
也就是说,“New Tab”不是一个内部字段,而是一个本地化的字符串。这意味着这一行必须更新,以获得该文本的正确本地化版本


很可能有更好的“定位可靠的东西”可以使用,但我对chrome automation不够熟悉,无法说出是否有,如果有什么。

你在哪一行得到错误?你的标题说你的错误是“值不能为空”-你的问题是“值不能为负”。这似乎是两件完全不同的事情。是哪一件?没有人有时间在没有任何建议的情况下分析您的代码,错误是在哪一行抛出的。。确切的错误消息是什么(请复制并粘贴它,因为您在键入时似乎无法弄清楚)?哪一行引发了异常?(堆栈跟踪告诉您这一点,您也可以通过在调试器中单步执行代码来找到它。)编辑的问题我解决了所有的评论。感谢大家指出主题。