2016-10-16 140 views
1

所以我想从谷歌浏览器(标题,URL)中提取打开的标签,并在chrome任务管理器中列出主题。 到目前为止,我已经试图过滤所有的镀铬工艺,并获得窗口标题,但不工作:如何从chrome获取打开的标签列表? | C#

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

这不给我地址或选项卡的标题,有另一种方式去做吧?

回答

1

第一参考这两个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); 
     } 
    } 
} 
+0

在线:AutomationElement elmTabStrip = treewalker.GetParent(elmNewTab); – user6879072

+0

出错或什么? – Mostafiz

+0

我收到一个错误:ArgumentNullException是未处理的 – user6879072

-1

它寻找一个工程中国语内容“新标签”,如果你的浏览器是不是英语也不会找到文本,而不是工作

+0

这里有什么意思? –

0

我真的不知道为什么你过于复杂这... 它的工作原理就像这样:

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