How to get a list of open tabs from chrome? | C#

前端 未结 4 956
抹茶落季
抹茶落季 2020-12-03 16:25

So I want to extract the open tabs from google chrome (title, URL) and list theme out kind of like in the chrome task manager. So far I have tried to filter all the chrome p

相关标签:
4条回答
  • 2020-12-03 16:51

    First Reference two dll

    UIAutomationClient.dll
    UIAutomationTypes.dll
    

    Located: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0 (or 3.5)

    Then

    using System.Windows.Automation;
    

    and the code

    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);
          // 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);
          }
       }
    }
    
    0 讨论(0)
  • 2020-12-03 16:55

    It's looking for a english content "new tab", if your browser it's not in english it will not find the text and not work

    0 讨论(0)
  • 2020-12-03 16:57

    You can try this:

    AutomationElement root = AutomationElement.FromHandle(process.MainWindowHandle);
    Condition condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
    var tabs = root.FindAll(TreeScope.Descendants, condition);
    
    0 讨论(0)
  • 2020-12-03 17:06

    didn't found a way to get all URLS, seems like only current active is possible... but here is my code sample to get all Tab Names even if you seperated them in different windows:
    you need

    UIAutomationClient.dll
    UIAutomationTypes.dll 
    

    added to your References as seen above and
    using System.Windowsw.Automation

    Process[] procsEdge = Process.GetProcessesByName("msedge");
    if (procsEdge.Length <= 0)
    {
       Console.WriteLine("Edge is not running");
    }
    else
    {
       foreach (Process proc in procsEdge)
       {
            //the Edge process must have a window
            if (proc.MainWindowHandle != IntPtr.Zero)
            {
                AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
                TreeWalker treewalker = TreeWalker.ControlViewWalker;
                AutomationElement rootParent = treewalker.GetParent(root);
                Condition condWindow = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window);
                AutomationElementCollection edges = rootParent.FindAll(TreeScope.Children, condWindow);
                Condition condNewTab = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
                foreach (AutomationElement e in edges)
                {
                    //check if the root element is named with *Edge*
                    if (e.Current.Name.Contains("Edge"))
                    {
                        //var beb = e.FindAll(TreeScope.Children, condNewTab);
                        foreach (AutomationElement tabitem in e.FindAll(TreeScope.Descendants, condNewTab))
                        {
                            Console.WriteLine("TABNAME: " + tabitem.Current.Name);
                        }
                    }
                }
            }
       }
    } 
    

    maybe that is helping somebody i realized to late that without the URL's it's not helping me...

    0 讨论(0)
提交回复
热议问题