2016-10-16 139 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); 
相關問題