我一直在想如何让Puppeteer使用,本地浏览器的数据,最终找到了办法

使用puppeteer-core

无头浏览器-min.gif

  1. const puppeteer = require("puppeteer-core");
  2. const fs = require('fs')
  3. const path = require('path')
  4. const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time));
  5. let browser,
  6. page;
  7. const getImg = async () => {
  8. if (! browser) {
  9. browser = await puppeteer.launch({
  10. // headless: false,
  11. // 这里的路径改成自己电脑上的路径
  12. executablePath: "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
  13. userDataDir: "C:\\Users\\81492\\AppData\\Local\\Google\\Chrome\\User Data",
  14. defaultViewport: {
  15. width: 1920,
  16. height: 1080
  17. },
  18. args: [
  19. // "--user-data-dir=C:\\Users\\81492\\AppData\\Local\\Google\\Chrome\\User Data",
  20. // "--no-sandbox",
  21. // "--disable-setuid-sandbox",
  22. // "--start-maximized",
  23. "--window-size=1920,1080",
  24. ],
  25. // ignoreDefaultArgs: ["--disable-extensions"],
  26. });
  27. }
  28. if (! page) {
  29. page = await browser.newPage();
  30. }
  31. await page.goto("https://www.yuque.com/liulinboyi", {waitUntil: "networkidle2"});
  32. await sleep(500);
  33. const cover = await page.waitForSelector(".homepage-header");
  34. await page.evaluate((el, value) => el.setAttribute("style", value), cover, "display: none");
  35. fs.rmSync(path.resolve(__dirname, './demo.png'))
  36. await page.screenshot({path: "demo.png", fullPage: true});
  37. await browser.close();
  38. };
  39. (async function() {
  40. await getImg()
  41. })()
  1. {
  2. "name": "my-puppeteer",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "index.js",
  6. "scripts": {
  7. "test": "echo \"Error: no test specified\" && exit 1",
  8. "start": "node puppeteer.js"
  9. },
  10. "keywords": [],
  11. "author": "",
  12. "license": "ISC",
  13. "dependencies": {
  14. "puppeteer-core": "^17.0.0"
  15. }
  16. }