Skip to main content

FileChooser

FileChooser 对象由 page.on('filechooser') 事件中的页面调度。

¥FileChooser objects are dispatched by the page in the page.on('filechooser') event.

// Start waiting for file chooser before clicking. Note no await.
const fileChooserPromise = page.waitForEvent('filechooser');
await page.getByText('Upload file').click();
const fileChooser = await fileChooserPromise;
await fileChooser.setFiles(path.join(__dirname, 'myfile.pdf'));

方法

¥Methods

element

Added in: v1.8 fileChooser.element

返回与此文件选择器关联的输入元素。

¥Returns input element associated with this file chooser.

用法

¥Usage

fileChooser.element();

返回

¥Returns


isMultiple

Added in: v1.8 fileChooser.isMultiple

返回此文件选择器是否接受多个文件。

¥Returns whether this file chooser accepts multiple files.

用法

¥Usage

fileChooser.isMultiple();

返回

¥Returns


page

Added in: v1.8 fileChooser.page

返回此文件选择器所属的页面。

¥Returns page this file chooser belongs to.

用法

¥Usage

fileChooser.page();

返回

¥Returns


setFiles

Added in: v1.8 fileChooser.setFiles

设置与此选择器关联的文件输入的值。如果某些 filePaths 是相对路径,则它们将相对于当前工作目录进行解析。对于空数组,清除选定的文件。

¥Sets the value of the file input this chooser is associated with. If some of the filePaths are relative paths, then they are resolved relative to the current working directory. For empty array, clears the selected files.

用法

¥Usage

await fileChooser.setFiles(files);
await fileChooser.setFiles(files, options);

参数

¥Arguments

文件名

¥File name

文件类型

¥File type

文件内容

¥File content

启动导航的操作正在等待这些导航发生并开始加载页面。你可以通过设置此标志来选择不等待。仅在特殊情况下才需要此选项,例如导航到无法访问的页面。默认为 false

¥Actions that initiate navigations are waiting for these navigations to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. Defaults to false.

最长时间(以毫秒为单位)。默认为 0 - 没有超时。可以通过配置中的 actionTimeout 选项或使用 browserContext.setDefaultTimeout()page.setDefaultTimeout() 方法更改默认值。

¥Maximum time in milliseconds. Defaults to 0 - no timeout. The default value can be changed via actionTimeout option in the config, or by using the browserContext.setDefaultTimeout() or page.setDefaultTimeout() methods.

返回

¥Returns