.net 使用C获取活动的ChromeURL#
我找到了这个解决方案:.net 使用C获取活动的ChromeURL#,.net,google-chrome,.net,Google Chrome,我找到了这个解决方案: Process[] procsChrome = Process.GetProcessesByName("chrome"); foreach (Process chrome in procsChrome) { // the chrome process must have a window if (chrome.MainWindowHandle == IntPtr.Zero)
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);
AutomationElementCollection elmUrlBars = elm.FindAll(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, ""));
// if it can be found, get the value from the URL bar
if (elmUrlBars != null)
{
foreach (AutomationElement item in elmUrlBars)
{
AutomationPattern[] patterns = item.GetSupportedPatterns();
if (patterns.Length > 0)
{
ValuePattern val = (ValuePattern)item.GetCurrentPattern(patterns[0]);
Console.WriteLine("Chrome URL found: " + val.Current.Value);
}
}
}
}
}
但在上一个chrome版本(34.0.1847.131 m)中,in不起作用。
有人能告诉我更常见的解决方案吗?在我的情况下试试这个解决方案,效果很好。
string GetChromeUrl(IntPtr hndl)
{
if (hndl.ToInt32() > 0)
{
string url = "";
AutomationElement elm = AutomationElement.FromHandle(hndl);
AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
if (elmUrlBar != null)
{
AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
if (patterns.Length > 0)
{
ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
url = val.Current.Value;
return url;
}
}
}
return "";
}
你想完成什么需要你阅读“活动Chrome URL”?我需要知道,当前的活动Chrome URL,我知道可以通过编写Chrome扩展来完成,但这不是我的解决方案。所有其他逻辑都在.net上。为什么这样做?请给出一点解释,因为这将最大限度地减少未经理解的复制和粘贴。StackOverflow(通常)不鼓励在没有任何解释的情况下进行代码转储。请参阅windows中的“inspect.exe”,其中找到了“LegacyIAccessible.Name”和“LegacyIAccessible.Value”属性,该属性的参数是“Address and search bar”,返回Chrome浏览器的活动URL。如何调用此方法?