Skip to main content
Version: 24.38.0

Page.waitForFileChooser() 方法

🌐 Page.waitForFileChooser() method

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

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

caution

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

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

caution

目前不支持拦截通过 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

范围

🌐 Parameter

类型

🌐 Type

描述

🌐 Description

options

WaitTimeoutOptions

(可选)

🌐 (Optional)

返回:

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']);