所以我想从谷歌浏览器(标题,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);
}
}
这并没有给我标签的地址或标题,还有另外一种方法吗?
答案 0 :(得分:3)
首先参考两个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);
}
}
}
答案 1 :(得分:1)
正在寻找英文内容“新标签”,如果您的浏览器不是英文,则无法找到文字而无法正常工作
答案 2 :(得分:0)
我真的不知道你为什么过于复杂这个...... 它的工作原理如下:
AutomationElement root = AutomationElement.FromHandle(process.MainWindowHandle);
Condition condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window);
var tabs = root.FindAll(TreeScope.Descendants, condition);
答案 3 :(得分:0)
找不到找到所有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的人,这对我没有帮助...