本文介紹了如何從 chrome 獲取打開的標簽列表?|C#的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
所以我想從 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#的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!