アクティビティ
最新
バナーの背景画像
UI Automation のアクティビティ
最終更新日 2024年4月26日

UI Automation API を使用してブラウザー検索を実行し、結果を取得する

このチュートリアルでは、Google 検索エンジンを開いて「UiPath」を検索し、UiPath の公式 Web サイトから結果を取得してコンソールに表示するオートメーションを、UI Automation API を使用して作成する方法について説明します。

前提条件

手順

  1. [新規] を選択してコード化されたワークフローを作成し、[ファイル] グループから [コード化されたワークフロー] を選択します。
  2. オブジェクト リポジトリ内のコード化されたワークフローで使用する、すべての UI 要素をキャプチャします。

    この例では、次の UI 要素をキャプチャする必要があります。

    • GoogleHomeScreen - Google のホーム画面です。ここで検索を実行します。
      • googleSearchBar - Google の検索バーです。ここにキーワード「UiPath」を入力します。
    • googleResultsScreen - 検索の実行後に Google の最初の結果を表示する画面です。
      • firstPageOfResults - 最初の結果のみを含む画面の部分です。
      • uipathOfficialWebsite - UiPath の公式 Web サイトに誘導する結果です。


  3. Google のホーム画面用の変数を作成し、Open API を使用してホーム画面を開きます。
    public class Search : CodedWorkflow
        {
            [Workflow]
            public void Execute()
            {
                // 1. Open Google
               var homeScreen = uiAutomation.Open(ObjectRepository.Descriptors.Google.GoogleHomeScreen);public class Search : CodedWorkflow
        {
            [Workflow]
            public void Execute()
            {
                // 1. Open Google
               var homeScreen = uiAutomation.Open(ObjectRepository.Descriptors.Google.GoogleHomeScreen);
  4. TypeInto API を使用して、Google の検索バーに「UiPath」と入力します。
    // 2. Type "UiPath" in the Google search bar
                homeScreen.TypeInto(ObjectRepository.Descriptors.Google.GoogleHomeScreen.googleSearchBar, "UiPath");// 2. Type "UiPath" in the Google search bar
                homeScreen.TypeInto(ObjectRepository.Descriptors.Google.GoogleHomeScreen.googleSearchBar, "UiPath");
  5. KeyboardShortcut API を使用して Enter のキーボード ショートカットを送信し、対応する結果を検索します。
    // 3. Send the "Enter" keyboard shortcut to search for the corresponding Google results
                homeScreen.KeyboardShortcut(ObjectRepository.Descriptors.Google.GoogleHomeScreen.googleSearchBar, "[d(hk)][k(enter)][u(hk)]");// 3. Send the "Enter" keyboard shortcut to search for the corresponding Google results
                homeScreen.KeyboardShortcut(ObjectRepository.Descriptors.Google.GoogleHomeScreen.googleSearchBar, "[d(hk)][k(enter)][u(hk)]");
  6. Attach API を使用して、Google の結果が表示された画面にフォーカスします。
    注: アプリケーション内の特定の画面をターゲットにする必要があり、その画面が既に Open API を使用して開かれている場合は、Open API ではなく Attach API を使用します。Attach API ではアプリケーションが不必要に起動されないため、オートメーションをスムーズに動作させることができます。
    var googleScreen = uiAutomation.Attach(ObjectRepository.Descriptors.Google.googleResultsScreen);var googleScreen = uiAutomation.Attach(ObjectRepository.Descriptors.Google.googleResultsScreen);
  7. GetText API を使用して Google の最初の結果からテキストを取得し、コンソールに出力します。
    string firstresultsText = googleScreen.GetText(ObjectRepository.Descriptors.Google.googleResultsScreen.firstPageOfResults);
                Console.WriteLine(firstresultsText);string firstresultsText = googleScreen.GetText(ObjectRepository.Descriptors.Google.googleResultsScreen.firstPageOfResults);
                Console.WriteLine(firstresultsText);
  8. 文字列「"https://www.uipath.com"」を含む最初の結果のテキストを出力する If 句を作成します。
    // Initialize with an empty string
            string officialWebsiteUiPath = "";
                if (firstresultsText.Contains("https://www.uipath.com"))
                {
                 officialWebsiteUiPath = googleScreen.GetText(ObjectRepository.Descriptors.Google.googleResultsScreen.uipathOfficialWebsite);
                }
                Console.WriteLine(officialWebsiteUiPath);// Initialize with an empty string
            string officialWebsiteUiPath = "";
                if (firstresultsText.Contains("https://www.uipath.com"))
                {
                 officialWebsiteUiPath = googleScreen.GetText(ObjectRepository.Descriptors.Google.googleResultsScreen.uipathOfficialWebsite);
                }
                Console.WriteLine(officialWebsiteUiPath);

結果

このオートメーションは、Google 検索エンジンを好みの Web ブラウザーで開いて「UiPath」を検索し、最初のページから結果を取得して、リンク https://uipath.com を含む結果からテキストを抽出します。

サンプル プロジェクト

この手順に従ってチュートリアルを自分で試してみるには、サンプル プロジェクト「Google Results using UI Automation API (UI Automation API を使用した Google の検索結果」をダウンロードします。

  • 前提条件
  • 手順
  • 結果
  • サンプル プロジェクト

Was this page helpful?

サポートを受ける
RPA について学ぶ - オートメーション コース
UiPath コミュニティ フォーラム
UiPath ロゴ (白)
信頼とセキュリティ
© 2005-2024 UiPath. All rights reserved.