如何从chrome获取打开的标签列表? | C# [英] How to get a list of open tabs from chrome? | C#
问题描述
到目前为止,我已经尝试过滤所有的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
位于: 然后 和代码 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: This doesn't give me the address or the title of the tab, is there another way to do it? First Reference two dll Located: Then and the code
这篇关于如何从chrome获取打开的标签列表? | C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 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);
}
}
}
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);
}
}
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);
}
}
}