Windows 10

起動中のMicrosoft EdgeからタイトルとURLを取得するC#コード(UI Automation編)

2017/8/18 追記:
当記事のコードは現在動作しなくなっているため、新しくコードを書き直しました。


Microsoft EdgeのDeveloper Resourcesを見てみると、“サポートが必要ならStack Overflowで聞いてね!”的なことが書いてあります。

そしてStack OverflowにはEdge関連の質問が集まっているわけですが、外部からの制御に関しては下記のような質問が寄せられています(2015/8/18 時点)。

特に「EdgeからURLとタイトルを取得したい」という質問では、Microsoftの中の人が回答をつけていて、UI Automationを使ったコードを紹介しています。

当ブログでもすでにこの記事で同様のコードを紹介していますが、.NETではコードを書いていなかったので、良い機会なのでC#でも同じ処理を行うコードを書いてみたいと思います。

※ UIAutomationClient, UIAutomationTypes 要参照

using System;
using System.Windows.Automation;

namespace UIAutomationEdge
{
  class Program
  {
    public static void Main(string[] args)
    {
      AutomationElement root = AutomationElement.RootElement;
      AutomationElement edge = root.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "TitleBar"));
      edge = edge.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "Windows.UI.Core.CoreWindow"));
      if (edge != null) {
        AutomationElement tabslist = edge.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "TabsList"));
        AutomationElementCollection items = tabslist.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "GridViewItem"));
        foreach (AutomationElement elm in items) {
          SelectionItemPattern sel = (SelectionItemPattern)elm.GetCurrentPattern(SelectionItemPattern.Pattern);
          sel.Select();
          AutomationElement address = edge.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "addressEditBox"));
          address.SetFocus();
          ValuePattern v = (ValuePattern)address.GetCurrentPattern(ValuePattern.Pattern);
          Console.WriteLine("Title:" + elm.Current.Name + ", URL:" + v.Current.Value);
        }
      }
    }
  }
}

AutomateMicrosoftEdge_UIA_Csharp_01

VBAに比べて大分スッキリしたコードになりました。
PowerShellでMicrosoft Edgeを操作する」でも書いている通り、EdgeのオートメーションにはSelenium + WebDriverがオススメですが、UI Automationを使う場合は.NETで書いた方が楽できるだろうと思います。

Microsoft Edgeを操作するVBScript前のページ

続・起動中のMicrosoft EdgeからタイトルとURLを取得するC#コード(UI Automation編)次のページ

関連記事

  1. Windows 10

    [Selenium]ExecuteScriptで指定した要素のIDを取得する。

    MSDN フォーラムにあった質問「Edge向けWebDriverでDO…

  2. Windows 10

    Microsoft Edgeのお気に入りをHTML形式でインポート・エクスポートする。

    以前当ブログでMicrosoft Edgeのお気に入りを編集する方法に…

  3. Windows 10

    [Windows 10]Microsoft Edgeでブックマークレットを使う。

    2017/3/29 追記:新しく記事を書き直しました。…

  4. Office関連

    【2017年1月版】Microsoft Edgeを操作するVBAマクロ(DOM編)(2)

    昨日の記事で、Microsoft Edgeを操作するVBAコードを改め…

  5. Windows関連

    ダウンロードフォルダーのパスを取得するVBScript

    ダウンロードフォルダーのパスを取得する必要があったので、過去に書いた記…

  6. Windows関連

    特殊フォルダーのパスを取得するVBScript

    ファイルのコピーや移動を行う場合に特殊フォルダーのパスが必要になること…

コメント

  • コメント (0)

  • トラックバックは利用できません。

  1. この記事へのコメントはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP