本文介绍了如何获取Chrome浏览器中所有选项卡的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿所有人,
我正在尝试创建一个程序,可以读取程序中要监视的所有打开的选项卡URL。我尝试了其他涉及Automation Element的解决方案,它给了我选项卡的名称,但不是URL。以下是我尝试使用的代码,但其他
建议会有所帮助。
I am trying to create a program that can read all open tabs urls to be monitored from within a program. Ive tried other solutions involving Automation Element and it give me the name of the tab but not the URL. Here is the code I am trying to use but other suggestions would help.
List<CHROMEWINDOW> GetChromeInfo()
{
Console.WriteLine(">> Getting Chrome Tabs");
List<CHROMEWINDOW> ChromeTabs = new List<CHROMEWINDOW>();
ChromeTabs.Clear();
Process[] procsChrome = Process.GetProcessesByName("chrome");
if (procsChrome.Length <= 0)
{
Console.WriteLine(">> Chrome not running");
return null;
}
else
{
foreach (Process proc in procsChrome)
{
if (proc.MainWindowHandle == IntPtr.Zero)
{
//Console.WriteLine(">> Main Window matches IntPtr Zero");
continue;
}
AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "New Tab");
AutomationElement elmNewTab = root.FindFirst(TreeScope.Descendants, condNewTab);
TreeWalker treewalker = TreeWalker.ControlViewWalker;
AutomationElement elmTabStrip = treewalker.GetParent(elmNewTab);
Condition condTabItem = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
foreach (AutomationElement tabitem in elmTabStrip.FindAll(TreeScope.Children, condTabItem))
{
Console.WriteLine(">> Adding Tab - {0}", tabitem.Current.Name);
CHROMEWINDOW win = new CHROMEWINDOW(proc, tabitem.Current.Name.ToString());
ChromeTabs.Add(win);
}
}
return ChromeTabs;
}
}
感谢您的回复!
- Nathan Hastings Interverse软件执行官
- Nathan Hastings Executive Of Interverse Software
推荐答案
您可能会有更多的运气在API供应商(谷歌)发布此处。
You will probably have more luck posting this at the API vendor (Google) then here.
这是我发现人们在制作场景时所做的事情:
This is what I found people are doing when having your scenario:
// there are always multiple chrome processes, so we have to loop through all of them to find the
// process with a Window Handle and an automation element of name "Address and search bar"
Process[] procsChrome = Process.GetProcessesByName("chrome");
foreach (Process chrome in procsChrome) {
// the chrome process must have a window
if (chrome.MainWindowHandle == IntPtr.Zero) {
continue;
}
// find the automation element
AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle);
AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
// if it can be found, get the value from the URL bar
if (elmUrlBar != null) {
AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
if (patterns.Length > 0) {
ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
Console.WriteLine("Chrome URL found: " + val.Current.Value);
}
}
}
这篇关于如何获取Chrome浏览器中所有选项卡的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!