So I am trying to gett all the open tabs from Chrome using UIAutomation in C# but I keep getting the error:
System.ArgumentNullException occurred
HResult=0x80004003
Message=Value can't be NULL.
Source=UIAutomationClientStackTrace:
at System.Windows.Automation.TreeWalker.GetParent(AutomationElement element)
at chromeTabsTest.Program.Main(String[] args) in C:\Users...\chromeTabsTest\chromeTabsTest\Program.cs:line 31
The error is indicated with a comment in the code.
    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();
        }
    }
}
This code is from another Stack Overflow question: question
It seems that the following line is language sensitive:
Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "New Tab");
That is to say that "New Tab" rather than being an internal field is a localised string. This means that this line must be updated to have the correctly localised version of this text.
It is quite possible that there is a better "locate something reliable" that could be used but I am not familiar enough with chrome automation to be able to say if there is and if so what.
User contributions licensed under CC BY-SA 3.0