我无法从 c# windows 表单应用程序中捕获 Microsoft edge url
I am not able to capture Microsoft edge url from c# windows forms application
我尝试了以下方法来获取 URL,但它仅适用于 windows 默认 Edge 浏览器,不适用于更新版本的 Edge 浏览器(Ver : 83.0)。
我正在使用这个浏览器:https://www.microsoft.com/en-us/edge
public static string GetEdgeUrl(Process process)
{
try
{
if (process == null)
throw new ArgumentNullException("process");
if (process.MainWindowHandle == IntPtr.Zero)
return null;
AutomationElement main = AutomationElement.FromHandle(process.MainWindowHandle);
if (main == null) // not edge
return null;
AutomationElement window = main.FindFirst(TreeScope.Children, new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
new PropertyCondition(AutomationElement.NameProperty, "Microsoft Edge")));
if (window == null) // not edge
return null;
var adressEditBox = window.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.AutomationIdProperty, "addressEditBox"));
return ((TextPattern)adressEditBox.GetCurrentPattern(TextPattern.Pattern)).DocumentRange.GetText(int.MaxValue);
}
catch (Exception ex)
{
throw ex;
}
}
终于找到答案了。这对我有用。我使用了与 Chrome 相同的技术。谢谢@stuartd
if (process == null)
throw new ArgumentNullException("process");
if (process.MainWindowHandle == IntPtr.Zero)
return null;
AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
if (element == null)
return null;
AutomationElement edit = element.FindFirst(TreeScope.Subtree,
new AndCondition(
new PropertyCondition(AutomationElement.NameProperty, "address and search bar", PropertyConditionFlags.IgnoreCase),
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)));
if (edit != null)
{
var i = ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
return i;
}
else
{
return "";
}
我尝试了以下方法来获取 URL,但它仅适用于 windows 默认 Edge 浏览器,不适用于更新版本的 Edge 浏览器(Ver : 83.0)。 我正在使用这个浏览器:https://www.microsoft.com/en-us/edge
public static string GetEdgeUrl(Process process)
{
try
{
if (process == null)
throw new ArgumentNullException("process");
if (process.MainWindowHandle == IntPtr.Zero)
return null;
AutomationElement main = AutomationElement.FromHandle(process.MainWindowHandle);
if (main == null) // not edge
return null;
AutomationElement window = main.FindFirst(TreeScope.Children, new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
new PropertyCondition(AutomationElement.NameProperty, "Microsoft Edge")));
if (window == null) // not edge
return null;
var adressEditBox = window.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.AutomationIdProperty, "addressEditBox"));
return ((TextPattern)adressEditBox.GetCurrentPattern(TextPattern.Pattern)).DocumentRange.GetText(int.MaxValue);
}
catch (Exception ex)
{
throw ex;
}
}
终于找到答案了。这对我有用。我使用了与 Chrome 相同的技术。谢谢@stuartd
if (process == null)
throw new ArgumentNullException("process");
if (process.MainWindowHandle == IntPtr.Zero)
return null;
AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
if (element == null)
return null;
AutomationElement edit = element.FindFirst(TreeScope.Subtree,
new AndCondition(
new PropertyCondition(AutomationElement.NameProperty, "address and search bar", PropertyConditionFlags.IgnoreCase),
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)));
if (edit != null)
{
var i = ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
return i;
}
else
{
return "";
}