【发布时间】:2020-07-06 10:18:54
【问题描述】:
从下面的代码中,我可以获得当前的 Chrome URL,但这需要时间。有时它需要超过 2 分钟,有时它没有返回任何值。
请指导我解决这个问题。
public string GetActiveTabUrl_Google_Chrome()
{
Process[] procsChrome = Process.GetProcessesByName("chrome");
if (procsChrome.Length <= 0)
return null;
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);
var SearchBar = root.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
if (SearchBar != null)
{
return (string)SearchBar.GetCurrentPropertyValue(ValuePatternIdentifiers.ValueProperty);
}
}
}
我搜索了很多,但没有得到确切的解决方案。我尝试了多台电脑,但发现同样的问题。
谢谢
【问题讨论】:
标签: c# winforms google-chrome