尝试将变量传递给 Puppeteer 中的参数

问题描述 投票:0回答:2

如何在 Puppeteer 中将变量传递给参数。我的最终目标是为 pdf 提供多个名称(90 多个名称),而无需键入每个文件名

var name = example1

await page.pdf({
        path: '/name/.pdf',
        printBackground: true,
      });
puppeteer
2个回答
0
投票

希望这有帮助,(您也可以使用字符串连接)

const names = ['example1', 'example2', 'example3'];
for (const name of names) {
    await page.pdf({
        path: `/${name}.pdf`,
        printBackground: true,
    });
}

0
投票

假设您想要打开每个 URL,然后将网页另存为 PDF 文件。您可以根据 URL 或网页标题确定文件名/路径,然后使用 @digbijay-gautam 方法保存 pdf 文件。以此页面为例,您可以使用以下文件名/路径:

{
  "url": "https://stackoverflow.com/questions/77744840/trying-to-pass-a-variable-to-a-parameter-in-puppeteer",
  "basename": "trying-to-pass-a-variable-to-a-parameter-in-puppeteer.pdf",
  "hostname_hashcode__MD5": "stackoverflow.com_981ce9411a44018e69819ca11425396b.pdf",
  "hostname_path_name": "stackoverflow.com_questions_77744840_trying-to-pass-a-variable-to-a-parameter-in-puppeteer.pdf",
  "hostnameHashcode__MD5": "stackoverflow.com/981ce9411a44018e69819ca11425396b.pdf",
  "hostnameHashcode__SHA256": "stackoverflow.com/fb13fa83cab3939483467d20dcc953583ae7a9b4c334c6a409cdc799ecce4f53.pdf",
  "hostnamePath_name": "stackoverflow.com/questions_77744840_trying-to-pass-a-variable-to-a-parameter-in-puppeteer.pdf",
  "hostnamePathname": "stackoverflow.com/questions/77744840/trying-to-pass-a-variable-to-a-parameter-in-puppeteer.pdf",
  "hashcode__MD5": "981ce9411a44018e69819ca11425396b.pdf",
  "path_name": "questions_77744840_trying-to-pass-a-variable-to-a-parameter-in-puppeteer.pdf",
  "pathname": "/questions/77744840/trying-to-pass-a-variable-to-a-parameter-in-puppeteer.pdf",
  "hostname_title": "stackoverflow.com_Trying to pass a variable to a parameter in Puppeteer - Stack Overflow.pdf",
  "hostnameTitle": "stackoverflow.com/Trying to pass a variable to a parameter in Puppeteer - Stack Overflow.pdf",
  "title": "Trying to pass a variable to a parameter in Puppeteer - Stack Overflow.pdf"
}
© www.soinside.com 2019 - 2024. All rights reserved.