C# 读取边缘浏览器标题&;带有System.Windows.Automation的Url
我正在尝试从Microsoft EDGE浏览器中读取标题和URL。 最好使用System.Windows.Automation执行此操作,因为代码库已将其用于其他问题C# 读取边缘浏览器标题&;带有System.Windows.Automation的Url,c#,ui-automation,microsoft-edge,microsoft-ui-automation,C#,Ui Automation,Microsoft Edge,Microsoft Ui Automation,我正在尝试从Microsoft EDGE浏览器中读取标题和URL。 最好使用System.Windows.Automation执行此操作,因为代码库已将其用于其他问题 是否可以使用System.Windows.Automation 如何访问URL 目前为止,我: AutomationId "TitleBar" ClassName "ApplicationFrameWindow" Name = [string] => Reading out this element gives me the
AutomationId "TitleBar"
ClassName "ApplicationFrameWindow"
Name = [string]
=> Reading out this element gives me the TITLE
=> Walking it's children, I find the item "addressEditBox":
AutomationId "addressEditBox"
ClassName "RichEditBox"
Name "Search or enter web address"
=> I always get back the string "Search or enter web address"
=> This is the control where the url is in, though it isn't updated as the user goes to a website, it always returns a fixed string.
代码:
var digger1 = AutomationElement.FromHandle(process.MainWindowHandle).RootElement.FindAll(TreeScope.Children, Condition.TrueCondition);
foreach(AutomationElement d1 in digger1 {
if(d1.Current.ClassName.Equals("ApplicationFrameWindow")) {
var digger2 = d1.FindAll(TreeScope.Children, Condition.TrueCondition);
foreach(AutomationElement d2 in digger2) {
if(d2.Current.ClassName.Equals("Windows.Ui.Core.CoreWindow")) {
var digger3 = d2.FindAll(TreeScope.Children, Condition.TrueCondition);
foreach(AutomationElement d3 in digger3) {
if(d3.Current.AutomationId.Equals("addressEditBox")) {
var url = d3.Current.Name;
return url;
}
}
}
}
}
}
你快到了。您只需要从addressEditBox元素获取。下面是一个完整的示例控制台应用程序,用于转储桌面上当前运行的所有Edge窗口:
class Program
{
static void Main(string[] args)
{
AutomationElement main = AutomationElement.FromHandle(GetDesktopWindow());
foreach(AutomationElement child in main.FindAll(TreeScope.Children, PropertyCondition.TrueCondition))
{
AutomationElement window = GetEdgeCommandsWindow(child);
if (window == null) // not edge
continue;
Console.WriteLine("title:" + GetEdgeTitle(child));
Console.WriteLine("url:" + GetEdgeUrl(window));
Console.WriteLine();
}
}
public static AutomationElement GetEdgeCommandsWindow(AutomationElement edgeWindow)
{
return edgeWindow.FindFirst(TreeScope.Children, new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
new PropertyCondition(AutomationElement.NameProperty, "Microsoft Edge")));
}
public static string GetEdgeUrl(AutomationElement edgeCommandsWindow)
{
var adressEditBox = edgeCommandsWindow.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.AutomationIdProperty, "addressEditBox"));
return ((TextPattern)adressEditBox.GetCurrentPattern(TextPattern.Pattern)).DocumentRange.GetText(int.MaxValue);
}
public static string GetEdgeTitle(AutomationElement edgeWindow)
{
var adressEditBox = edgeWindow.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.AutomationIdProperty, "TitleBar"));
return adressEditBox.Current.Name;
}
[DllImport("user32")]
public static extern IntPtr GetDesktopWindow();
}
谢谢,我会尽快试用!我未能理解System.Windows.Automation API,我猜:)。。。谢谢。此方法仅在边缘窗口最大化时有效。你们能建议如何使它在边缘最小化的情况下工作吗?因为UIA不会在边缘最小化的情况下看到它