大网数据广告活动
Web 标准与前端开发

`input.setFiles` command

· 52CE 我爱测

The `input.setFiles` command of the `input` module simulates a file picker dialog by setting the file selection of an `<input type="file">` element in a given context to the specified file paths.

The input.setFiles command of the input module simulates a file picker dialog by setting the file selection of an <input type="file"> element in a given context to the specified file paths.

Syntax

{
  "method": "input.setFiles",
  "params": {
    "context": "5f07e3ca-ecac-465e-b9ef-49000c196ecf",
    "element": {
      "sharedId": "3be28343-afd3-4dea-a2b6-a863fbbb80e1"
    },
    "files": ["/home/user/documents/report.pdf"]
  }
}

Parameters

The params field contains:

  • context
  • element
  • files
    • : An array of strings, where each string is the absolute file path of a file to select. This command overrides any previously selected files. You can pass an empty array to clear the selection. If the array contains more than one file path, ensure that the <input type="file"> element has the multiple attribute.

Return value

The result field in the response is an empty object ({}).

Errors

  • invalid argument
    • : A required parameter is missing or has an invalid type.
  • no such element
    • : The element reference cannot be resolved to a valid DOM element in the given context.
  • no such frame
    • : No context with the given context ID is found.
  • unable to set file input
    • : The element is not an <input> element with type="file", the element is disabled, or more than one file path is provided without the multiple attribute.
  • unsupported operation
    • : The browser is unable to set the selected files to the provided paths; for example, if any of the specified files do not exist on the filesystem.

Examples

Setting a file on a file input

With a WebDriver BiDi connection and an active session, first obtain the sharedId of an <input type="file"> element using browsingContext.locateNodes or an input.fileDialogOpened event, then send the following message to set its selected file:

{
  "id": 1,
  "method": "input.setFiles",
  "params": {
    "context": "5f07e3ca-ecac-465e-b9ef-49000c196ecf",
    "element": {
      "sharedId": "3be28343-afd3-4dea-a2b6-a863fbbb80e1"
    },
    "files": ["/home/user/documents/test-upload.txt"]
  }
}

The browser responds as follows:

{
  "id": 1,
  "type": "success",
  "result": {}
}

Clearing the file selection

To clear all previously selected files, pass an empty array:

{
  "id": 2,
  "method": "input.setFiles",
  "params": {
    "context": "5f07e3ca-ecac-465e-b9ef-49000c196ecf",
    "element": {
      "sharedId": "3be28343-afd3-4dea-a2b6-a863fbbb80e1"
    },
    "files": []
  }
}

The browser responds as follows:

{
  "id": 2,
  "type": "success",
  "result": {}
}

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。