[英]I am not able to capture Microsoft edge url from c# windows forms application
我尝试了以下方法来获取 URL,但它仅适用于 windows 默认 Edge 浏览器,不适用于 Edge 浏览器的更新版本(版本: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 "";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.