如何从chrome获取打开的标签列表? | C# [英] How to get a list of open tabs from chrome? | C#

查看:301
本文介绍了如何从chrome获取打开的标签列表? | C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想从Chrome浏览器(标题,URL)中提取打开的标签,并在Chrome任务管理器中列出主题。
到目前为止,我已经尝试过滤所有的chrome进程并获得窗口标题,但这不起作用:

  var procs = Process.GetProcesses(); 



foreach(proc中的var proc)
{
if(Convert.ToString(proc.ProcessName)==chrome )
{
Console.WriteLine({0}:{1} | {2} | {3} ||| {4} \\\
,i,proc.ProcessName,runtime,proc .MainWindowTitle,proc.Handle);
}
}

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

b $ b

  UIAutomationClient.dll 
UIAutomationTypes.dll

位于: C:\ Program Files(x86)\ Reference Assembly \ Microsoft \ Framework >

然后

 使用System.Windows.Automation; 

和代码

  Process [] procsChrome = Process.GetProcessesByName(chrome); 
if(procsChrome.Length <= 0)
{
Console.WriteLine(Chrome is not running);
}
else
{
foreach(procsChrome中的Process proc)
{
// chrome进程必须有一个窗口
if( proc.MainWindowHandle == IntPtr.Zero)
{
continue;
}
//找到我们首先需要定位的选项卡 - 新选项卡按钮
AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
条件condNewTab = new PropertyCondition(AutomationElement.NameProperty,New Tab);
AutomationElement elmNewTab = root.FindFirst(TreeScope.Descendants,condNewTab);
//通过获取'new tab'按钮的父项来获取tabstrip
TreeWalker treewalker = TreeWalker.ControlViewWalker;
AutomationElement elmTabStrip = treewalker.GetParent(elmNewTab);
//遍历所有选项卡并获取页面标题的名称
条件condTabItem = new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.TabItem);
foreach(AutomationElement tabitem in elmTabStrip.FindAll(TreeScope.Children,condTabItem))
{
Console.WriteLine(tabitem.Current.Name);
}
}
}


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 processes and get the window titles but that doesn't work:

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

This doesn't give me the address or the title of the tab, is there another way to do it?

解决方案

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

这篇关于如何从chrome获取打开的标签列表? | C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆