Skip to main content
Version: 22.9.0

FileChooser 类

¥FileChooser class

文件选择器可让你对请求文件的页面做出反应。

¥File choosers let you react to the page requesting for a file.

签名:

¥Signature:

export declare class FileChooser

备注

¥Remarks

FileChooser 实例通过 Page.waitForFileChooser() 方法返回。

¥FileChooser instances are returned via the Page.waitForFileChooser() method.

在浏览器中,一次只能打开一个文件选择器。所有文件选择器都必须被接受或取消。不这样做将阻止后续文件选择器出现。

¥In browsers, only one file chooser can be opened at a time. All file choosers must be accepted or canceled. Not doing so will prevent subsequent file choosers from appearing.

此类的构造函数被标记为内部构造函数。第三方代码不应直接调用构造函数或创建扩展 FileChooser 类的子类。

¥The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the FileChooser class.

示例

¥Example

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

方法

¥Methods

方法

修饰符

描述

accept(paths)

接受带有给定文件路径的文件选择器请求。

评论:

这不会验证文件路径是否存在。另外,如果路径是相对路径,则根据 当前工作目录 进行解析。对于连接到远程 Chrome 环境的本地脚本,路径必须是绝对路径。

cancel()

关闭文件选择器而不选择任何文件。

isMultiple()

文件选择器是否允许选择 multiple 文件。