How to get a list of open tabs from chrome? | C#(如何从 chrome 获取打开的标签列表?|C#)
问题描述
所以我想从 google chrome 中提取打开的标签(标题、URL)并列出主题,就像在 chrome 任务管理器中一样.到目前为止,我已尝试过滤所有 chrome 进程并获取窗口标题,但这不起作用:
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}
", 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?
推荐答案
先引用两个dll
UIAutomationClient.dll
UIAutomationTypes.dll
位于:C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.0(或 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);
}
}
}
这篇关于如何从 chrome 获取打开的标签列表?|C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 chrome 获取打开的标签列表?|C#
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01