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

时间:2019-03-30 09:15:45

标签: javascript node.js puppeteer

我通过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();

1 个答案:

答案 0 :(得分:2)

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

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

let b = await page.$('#submit');
b.click();
相关问题