活动
最新
False
横幅背景图像
用户界面自动化活动
上次更新日期 2024年4月26日

使用用户界面自动化 API 执行浏览器搜索并检索结果

本教程说明如何使用用户界面自动化 API 创建自动化,以打开 Google 搜索引擎,执行 UiPath 搜索,从官方 UiPath 网站检索结果并将其显示在控制台中。

先决条件

  • UiPath Studio 2023.10 或更高版本
  • UiPath.UIAutomation.Activities 23.10.3 或更高版本
  • 访问在编码自动化中使用对象,了解在编码自动化中使用对象存储库元素的方法。

步骤

  1. 通过选择“新建”,然后从“文件”组中选择“编码工作流”,创建一个编码工作流。
  2. 捕获要在对象存储库内的编码工作流中使用的所有用户界面元素。

    对于此示例,您需要捕获以下用户界面元素:

    • GoogleHomeScreen - Google 主屏幕,您可以在其中执行搜索。
      • googleSearchBar - Google 搜索栏,您可以在其中输入 UiPath 关键字。
    • googleResultsScreen - 执行搜索后显示前几个 Google 结果的屏幕。
      • firstPageOfResults - 屏幕上仅包含前几个结果的部分。
      • uipathOfficialWebsite - 会将您定向至 UiPath 官方网站的结果。


  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. 发送 Enter 键盘快捷方式以使用 KeyboardShortcut API 搜索相应的结果。
    // 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 打开的应用程序中的特定屏幕时,请使用 Attach API,而不是 Open 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. 创建一个 If 子句,该子句会打印第一个结果,其中包含 "https://www.uipath.com" 字符串的文本。
    // 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 搜索引擎,执行 UiPath 搜索,从初始页面检索结果,并从结果中提取包含以下链接的文本:https://uipath.com

示例项目

要按照本教程的步骤操作并自行尝试,您可以下载以下示例项目:使用用户界面自动化 API 的 Goggle 结果

  • 先决条件
  • 步骤
  • 结果
  • 示例项目

此页面是否有帮助?

获取您需要的帮助
了解 RPA - 自动化课程
UiPath Community 论坛
Uipath 白色徽标
信任与安全
© 2005-2024 UiPath. All rights reserved.