如何从chrome获取打开的标签列表? | C#

所以我想从谷歌浏览器(标题,URL)和列表主题中提取打开的选项卡,就像在chrome任务管理器中一样。 到目前为止,我已尝试过滤所有chrome进程并获取窗口标题,但这不起作用:

var procs = Process.GetProcesses(); ... foreach (var proc in procs) { if (Convert.ToString(proc.ProcessName) == "chrome") { Console.WriteLine("{0}: {1} | {2} | {3} ||| {4}\n", i, proc.ProcessName, runtime, proc.MainWindowTitle, proc.Handle); } } 

这不会给我标签的地址或标题,还有另一种方法吗?

首先参考两个dll

 UIAutomationClient.dll UIAutomationTypes.dll 

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

然后

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

它正在寻找一个英文内容“新标签”,如果您的浏览器不是英文,它将找不到文本而不能正常工作

我真的不知道你为什么过于复杂…它的工作原理如下:

 AutomationElement root = AutomationElement.FromHandle(process.MainWindowHandle); Condition condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window); var tabs = root.FindAll(TreeScope.Descendants, condition);