如何从 chrome 获取打开的标签列表? | C#
Posted
技术标签:
【中文标题】如何从 chrome 获取打开的标签列表? | C#【英文标题】:How to get a list of open tabs from chrome? | C# 【发布时间】:2016-10-16 13:19:09 【问题描述】:所以我想从 google chrome 中提取打开的标签(标题、URL)并列出主题,就像在 chrome 任务管理器中一样。 到目前为止,我已尝试过滤所有 chrome 进程并获取窗口标题,但这不起作用:
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);
【讨论】:
在行:AutomationElement elmTabStrip = treewalker.GetParent(elmNewTab); 我收到一个错误:ArgumentNullException 未处理 其实我一切都很好,你能调试你的代码看看发生了什么吗? 我不知道问题出在哪里:“elmNewTab”由于某种原因为空并且未处理,但它怎么可能对您有用?扩展或其他东西会干扰吗? 从已删除的答案中复制评论(信用 Fanari):“错误是由Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "New Tab");
引起的,它想在其中找到“新标签”,但我找不到它,但我think for you too 是出于以下原因:该字符串已本地化,因此如果您有非英语 chrome,它将是翻译成该语言的“新标签”字符串。”【参考方案2】:
你可以试试这个:
AutomationElement root = AutomationElement.FromHandle(process.MainWindowHandle);
Condition condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
var tabs = root.FindAll(TreeScope.Descendants, condition);
【讨论】:
【参考方案3】:它正在寻找一个英文内容“新标签”,如果您的浏览器不是英文的,它将找不到文本并且不起作用
【讨论】:
【参考方案4】:没有找到获取所有 URL 的方法,似乎只有当前活动是可能的... 但这是我的代码示例,用于获取所有选项卡名称,即使您将它们分隔在不同的窗口中: 你需要
UIAutomationClient.dll
UIAutomationTypes.dll
如上所示添加到您的参考文献中和using System.Windowsw.Automation
Process[] procsEdge = Process.GetProcessesByName("msedge");
if (procsEdge.Length <= 0)
Console.WriteLine("Edge is not running");
else
foreach (Process proc in procsEdge)
//the Edge process must have a window
if (proc.MainWindowHandle != IntPtr.Zero)
AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
TreeWalker treewalker = TreeWalker.ControlViewWalker;
AutomationElement rootParent = treewalker.GetParent(root);
Condition condWindow = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window);
AutomationElementCollection edges = rootParent.FindAll(TreeScope.Children, condWindow);
Condition condNewTab = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
foreach (AutomationElement e in edges)
//check if the root element is named with *Edge*
if (e.Current.Name.Contains("Edge"))
//var beb = e.FindAll(TreeScope.Children, condNewTab);
foreach (AutomationElement tabitem in e.FindAll(TreeScope.Descendants, condNewTab))
Console.WriteLine("TABNAME: " + tabitem.Current.Name);
也许这对我迟到的人有所帮助,如果没有 URL,它对我没有帮助...
【讨论】:
这是唯一有效的示例(而接受的答案无效),但前提是 Edge 未最小化。以上是关于如何从 chrome 获取打开的标签列表? | C#的主要内容,如果未能解决你的问题,请参考以下文章
在 Firefox/Chrome 上打开新标签页或窗口时,如何获得新的浏览器会话?
如何使用硒网络驱动程序中的ruby脚本在chrome中打开新标签[重复]