尝试在Chrome中打印打开的标签时,System.Windows.Automation上的ArgumentNullException

时间:2016-10-16 16:43:09

标签: c# google-chrome tabs

这段代码应该正常工作,至少它对其他人有用,但我一直得到同样的ArgumentNullException was unhandled错误?可能是什么问题?

该应用程序应该打印出谷歌浏览器中的打开标签。尝试运行它,看看它是否适合你。

我导入了UIAtomationClient.dllUIAutomationTypes.dll

 using System.Windows.Automation;
...
    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 on this line

                        // 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);
                        }

                    }
                }

1 个答案:

答案 0 :(得分:0)

我不是这个自动化专家的专家,但这里有工作

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Automation;

namespace ConsoleApplication3
{
    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 condTabs = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button);
                    //Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "new tab");
                    AutomationElementCollection col = root.FindAll(TreeScope.Descendants, condTabs);
                    AutomationElement elmNewTab = col[4]; // allways the position 4 is new tab button

                    // get the tabstrip by getting the parent of the 'new tab' button 
                    TreeWalker treewalker = TreeWalker.ControlViewWalker;
                    AutomationElement elmTabStrip = treewalker.GetParent(elmNewTab); // <- Error on this line

                   // 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.ReadKey();
                }
            }
        }
    }
}

我查找窗口中的每个按钮,结果是在最小化,恢复和关闭按钮之前,新选项卡始终位于第4位。我知道并不是一个最简洁的解决方案来加入一个幻数,但它可以完成这项任务。

你走了!我在VS2015中进行了测试

working :S