【发布时间】:2018-11-06 12:27:20
【问题描述】:
我正在尝试自动化一个工作流程,其中我有一个目录中的文件列表并将它们放在一个数组中。然后对于数组中的每个文件,我调用一个 Chrome 自动化函数。
const path = require('path');
const chalk = require('chalk');
const puppeteer = require('puppeteer');
module.exports = {
generateOutput : async(fileName, url = "https://example.com/") => {
const filePath = path.join(process.cwd(), fileName);
const outputFilePath = path.join(process.cwd(), "OutputFiles");
try{
const browser = await puppeteer.launch({headless: false});
process.setMaxListeners(0);
const page = await browser.newPage();
await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: outputFilePath});
page.on('dialog', async dialog => {
console.log(chalk.magenta("Error Occured: " + dialog.message()));
await dialog.dismiss();
await browser.close();
});
await page.goto(url, {waitUntil: 'networkidle2'});
await page.click('#ui-id-9');
await page.click('#ui-id-18');
await page
.waitForSelector('#ui-id-9')
.then(() => console.log(chalk.magenta("Uploader module visible... Uploading the files") ));
const input = await page.$('#upload-file');
await input.uploadFile(filePath);
await page.waitFor(10000);
await page.click("#up-file");
await page.waitFor(50000);
await page
.waitForSelector('#ui-id-18')
.then(() => console.log(chalk.magenta("Downloader module visible... Downloading the files") ));
await page.click("#download-td");
await page.waitFor(100000);
await browser.close();
}
catch(e){
console.log(chalk.red(fileName + ' has failed in conversion.'));
}
}
};
这会同时创建一个 chrome 实例(比如 100 个文件对应 100 个)。有没有办法限制异步进程。 我没有多少经验。在 Node 中,所以我无法搜索正确的术语。
【问题讨论】:
标签: javascript node.js windows async-await puppeteer