FileChooser
FileChooser 对象由页面通过 page.on('filechooser') 事件派发。
// 在点击前开始等待文件选择器。注意这里没有 await。
const fileChooserPromise = page.waitForEvent('filechooser');
await page.getByText('上传文件').click();
const fileChooser = await fileChooserPromise;
await fileChooser.setFiles(path.join(__dirname, 'myfile.pdf'));
方法
element
v1.9 之前添加返回与此文件选择器关联的输入元素。
用法
fileChooser.element();
返回值
isMultiple
v1.9 之前添加返回此文件选择器是否接受多个文件。
用法
fileChooser.isMultiple();
返回值
page
v1.9 之前添加返回此文件选择器所属的页面。
用法
fileChooser.page();
返回值
setFiles
v1.9 之前添加设置与此文件选择器关联的文件输入框的值。如果某些 filePaths
是相对路径,则会相对于当前工作目录进行解析。传入空数组则会清除已选文件。
用法
await fileChooser.setFiles(files);
await fileChooser.setFiles(files, options);
参数
files
string | Array<string> | Object | Array<Object>#options
Object (可选)-
已弃用
此选项无效。
此选项无效。
-
最大等待时间(毫秒)。默认为
0
- 无超时。默认值可以通过配置中的actionTimeout
选项修改,或者使用 browserContext.setDefaultTimeout() 或 page.setDefaultTimeout() 方法设置。
-
返回