Skip to main content
Version: 23.2.0

Page.waitForFileChooser() 方法

¥Page.waitForFileChooser() method

此方法通常与触发文件选择的操作结合使用。

¥This method is typically coupled with an action that triggers file choosing.

提醒

必须在启动文件选择器之前调用此函数。它不会返回当前活动的文件选择器。

¥This must be called before the file chooser is launched. It will not return a currently active file chooser.

提醒

目前不支持拦截通过 DOM API(例如 window.showOpenFilePicker)触发的文件对话框。

¥Interception of file dialogs triggered via DOM APIs such as window.showOpenFilePicker is currently not supported.

签名

¥Signature

class Page {
abstract waitForFileChooser(
options?: WaitTimeoutOptions
): Promise<FileChooser>;
}

参数

¥Parameters

范围

类型

描述

options

WaitTimeoutOptions

(可选的)

Returns:

Promise<FileChooser>

备注

¥Remarks

在 "有头" 浏览器中,此方法会为用户生成原生文件选择器对话框 not showing up

¥In the "headful" browser, this method results in the native file picker dialog not showing up for the user.

示例

¥Example

以下示例单击一个发送文件选择器的按钮,然后使用 /tmp/myfile.pdf 进行响应,就好像用户已选择该文件一样。

¥The following example clicks a button that issues a file chooser and then responds with /tmp/myfile.pdf as if a user has selected this file.

const [fileChooser] = await Promise.all([
page.waitForFileChooser(),
page.click('#upload-file-button'),
// some button that triggers file selection
]);
await fileChooser.accept(['/tmp/myfile.pdf']);