2015-08-25 44 views
2

我试图从微软EDGE浏览器读出标题& URL。 最好的办法是使用System.Windows.Automation,因为代码库已经将其用于其他问题。用System.Windows.Automation读出边缘浏览器标题和网址

  1. System.Windows.Automation有可能吗?
  2. 如何访问URL?

我目前这个地步:

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; 
         } 
        } 
       } 
      } 
      } 
     } 

回答

4

你几乎没有。您只需要从addressEditBox元素中获取TextPattern。这里是一个完整的示例控制台应用程序,倾倒出桌面上所有当前正在运行的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(); 
} 
+0

谢谢,请尽快尝试一下!我无法理解System.Windows.Automation API我猜:)...谢谢。 –

+0

此方法仅在EDGE窗口最大化时起作用。如果EDGE最小化,您能建议如何使它工作吗? –

+0

因为如果UIA最小化,UIA不会看到边缘 –