You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Electron中Puppeteer调用报错:Cannot read properties of undefined (reading 'launch')

Electron + Puppeteer 报错:Cannot read properties of undefined (reading 'launch')

我在学习Electron.js构建网页数据抓取应用时,遇到以下错误:
Uncaught (in promise) Error: Cannot read properties of undefined (reading 'launch')

项目文件夹结构包含主进程文件、preload.js、func.js,以及两个HTML页面(收集信息的表单页和执行抓取的choose.html)。

相关代码

preload.js

const { contextBridge } = require("electron");
const { puppeteer } = require("puppeteer");

contextBridge.exposeInMainWorld("puppeteer", {
  launch: async (options) => {
    const browser = await puppeteer.launch(options);
    return browser;
  },
  newPage: async (options) => {
    const browser = await puppeteer.launch(options).newPage();
    return browser;
  },
});

func.js(数据抓取主函数)

const main_link = async (url) => {
  const browser = await puppeteer.launch();
  const page = await puppeteer.newPage();
  await page.goto(url);
  // await page.screenshot({ path: "screenshot.png" });
  const content = await page.evaluate(() => {
    let problem = document.querySelectorAll(".card .text-right a");
    let links = [];
    problem.forEach((element) => {
      links.push(element.href);
    });
    return links;
  });
  await browser.close();
  console.log(content);
};

main_link("https://example.com");

choose.html(执行抓取的页面)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Security-Policy" content="script-src 'self'" />
    <title>Choose</title>
    <script src="func.js" defer></script>
  </head>
  <body></body>
</html>

项目逻辑:第一个HTML页面是收集姓名、年龄的表单,点击按钮可跳转至choose.html;choose.html中引入func.js执行数据抓取逻辑。我尝试解决该错误但未果,因对Electron和后端技术不够熟练,希望能得到帮助。

内容的提问来源于stack exchange,提问作者kasparovian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 07:08:11