activities
latest
false
重要 :
请注意,此内容已使用机器翻译进行了部分本地化。 新发布内容的本地化可能需要 1-2 周的时间才能完成。
UiPath logo, featuring letters U and I in white

用户界面自动化活动

上次更新日期 2026年2月24日

插入 Js 脚本

UiPath.Core.Activities.InjectJsScript

在与用户界面元素对应的网页环境中执行 JavaScript 代码。

属性

输入
  • “脚本代码”- 要运行的 JavaScript 代码。可在此处将其写作字符串形式,或添加包含待执行代码的 .js 文件的完整路径。脚本代码应包含单个匿名函数,如以下示例所示:

    function (element, input) {
      return "result";
    }
    function (element, input) {
      return "result";
    }
    
    async function (element, input) 
    {
      const asyncResult = await new Promise((resolvePromiseCb) => 
      {
        const timeoutMs = 100;
        setTimeout(() => 
        {
          resolvePromiseCb("async_result");
        }, 
        timeoutMs);
      });
      return asyncResult;
    }
    async function (element, input) 
    {
      const asyncResult = await new Promise((resolvePromiseCb) => 
      {
        const timeoutMs = 100;
        setTimeout(() => 
        {
          resolvePromiseCb("async_result");
        }, 
        timeoutMs);
      });
      return asyncResult;
    }
    

    其中 element 表示与调用用户界面节点对应的 HTML 元素,input 表示调用者提供的输入字符串。

  • “输入参数”- JavaScript 代码的输入数据,为字符串或字符串变量形式。

  • “Target.Selector”- 用于在执行活动时查找特定用户界面元素的“文本”属性。它实际上是 XML 片段,用于指定要查找的图形用户界面元素及其一些父元素的属性。

  • “Target.Timeout(毫秒)”- 指定最长等待时间(以毫秒为单位),如果超出该时间后活动并未运行,系统便会抛出“SelectorNotFoundException”错误。默认值为 30000 毫秒(30 秒)。

  • “Target.WaitForReady”- 待目标准备就绪后,再执行操作。可用的选项如下:

    • “无”- 在执行操作之前,系统不会等待显示除目标用户界面元素之外的任何内容。例如,如果希望只从网页检索文本或单击特定按钮,则可以使用此选项,而不必等待用户界面元素全部加载。请注意,如果按钮依赖尚未加载的元素(如脚本),这可能会产生不良后果。
    • “交互/完成” - 待目标应用程序中的所有用户界面元素全部显示之后,再实际执行操作。 为了评估应用程序处于“交互”状态还是“完成”状态,系统将验证以下标签:
    • “桌面应用程序”- 系统会发出一条“wm_null”消息,以检查是否存在“<wnd>”、“<ctrl>”、“<java>”或“<uia>”标签。如存在,则执行活动。
    • 网页应用程序:
    1. Internet Explorer - The <webctrl> tag is used to check if the Ready state of the HTML document is set to Complete. Additionally, the Busy state has to be set to "False".
    2. Others - The <webctrl> tag is used to check if the Ready state of the HTML document is Complete.
    • “SAP 应用程序”- 首先验证是否存在“<wnd>”标签,然后使用 SAP 特定 API 来检测会话是否繁忙。
  • “Target.Element”- 使用另一个活动返回的“用户界面元素”变量。该属性不能与“选取器”属性一起使用。该字段仅支持用户界面元素变量。

  • “Target.ClippingRegion”- 按照左、上、右、下四个方向定义相对于用户界面元素的剪切矩形(以像素为单位)。支持正数和负数。

输出
  • “脚本输出”- JavaScript 代码返回的字符串结果。
常见
  • “显示名称”- 活动的显示名称。

  • ContinueOnError - Specifies if the automation should continue even when the activity throws an error. This field only supports Boolean values (True, False). The default value is False. As a result, if the field is blank and an error is thrown, the execution of the project stops. If the value is set to True, the execution of the project continues regardless of any error.

    备注:

    如果“Try Catch 异常处理”中包含该活动且“出错时继续”属性的值为“True”,则系统在执行项目时不会捕获任何错误。

其他
  • “私有”- 选中后将不再以“Verbose”级别记录变量和参数的值。
选项
  • Execution world - The JavaScript environment for the script execution.
    • Isolated - Allows access to the HTML elements, but prevents access to page variables and code. Use this option to ensure that the script execution does not conflict with the page.
    • “页面” - 允许访问 HTML 元素、页面变量和代码。如果您需要访问页面变量(例如jQuery $ )或与页面代码交互(例如window.alert ),请使用此选项。要修复特定于 MV3 扩展程序的这一已知问题,需要使用此选项。

您可以通过“项目设置” 更改所有“注入 JS 脚本” 活动的“执行世界” 属性。

备注:

The Execution world option is available starting with UiPath.UIAutomation.Activities version 22.12.

使用“注入 JS 脚本”活动的示例

注入 JS 脚本”活动用于打开浏览器并处理这些页面中的 .js 文件。您必须安装并启用浏览器扩展程序。

为了举例说明如何使用此活动,我们创建了一个示例,该示例将打开浏览器并显示带有用户指定文本的警示。

此自动化流程的构建方法如下:

  1. 打开 Studio ,创建一个新“流程”
    备注:

    确保将 .js 文件放置在项目文件夹中。在此示例中,我们使用了文件 example.js

  2. Drag a Sequence to the Workflow Designer.
    • 创建以下变量:

      变量名称变量类型默认值
      TextToWrite泛型值
  3. 将“输入对话框”活动拖动到“序列”容器内。
    • In the Properties panel, add the name Ask for message in the DisplayName field.
    • 在“标签”字段中添加表达式“"Please insert the text to be written on the web page."”。
    • 在“标题”字段中添加“"Please insert text"”消息。
    • 在“结果”字段中添加 textToWrite 变量。
  4. 在“输入对话框”活动下方放置一个“打开浏览器”活动。
    • 在“URL”字段中添加地址 "https://www.google.com/"
  5. 将“注入 JS 脚本”拖动到“打开浏览器”活动的“正文”容器中。
    • 在“属性”面板中,向“显示名称”字段添加名称“Show Alert”。
    • 在“输入参数”字段中添加变量 TextToWrite
    • 在“脚本代码”字段中添加表达式“"Inject JS\example.js"”。
  6. 运行流程。机器人将打开一个新的浏览器页面,并为警示提供您添加的文本。


  • 属性
  • 使用“注入 JS 脚本”活动的示例

此页面有帮助吗?

获取您需要的帮助
了解 RPA - 自动化课程
UiPath Community 论坛
Uipath Logo
信任与安全
© 2005-2026 UiPath。保留所有权利。