如何关闭 Node.js 中的文件对话框?

How to close a file dialog box in Node.js?

我按node.js打开文件对话框为

await page.goto('https://www.example.com', { waitUntil: 'networkidle0' });
let a = await page.$('#file');
a.click();

html 代码就像

<form action="">
<input type="file" id="file" />
<input type="submit" id="submit" value="Submit" />
</form>

a.click() 在浏览器中打开一个对话框来选择文件。当对话框打开时,第一个文件已经被选中,如果按enter,对话框将关闭。

我想知道 Node.js 是否有可能以编程方式关闭文件对话框?

换句话说,我想自动提交表单为

let a = await page.$('#file');
a.click();
// HERE close the opened dialog box
let b = await page.$('#submit');
b.click();

您无法打开 select 文件的对话框。相反,您必须使用 elementHandle.uploadFile 来处理文件上传:

let a = await page.$('#file');
await a.uploadFile('PATH/TO/YOUR.FILE');

let b = await page.$('#submit');
b.click();