Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
979 views
in Technique[技术] by (71.8m points)

c# - Reading out Edge Browser Title & Url with System.Windows.Automation

I'm trying to read out the TITLE & URL from the Microsoft EDGE Browser. Doing this with System.Windows.Automation most preferably since the code base already uses this for other problems.

  1. Is it possible with System.Windows.Automation?
  2. How to access the URL?

I'm currently this far:

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.

In code:

   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;
                      }
                   }
                }
             }
          }
       }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're almost there. You just need to get the TextPattern from the addressEditBox element. Here is a full sample Console app that dumps out all currently running Edge's windows on the desktop:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...