我一直在想如何让Puppeteer使用,本地浏览器的数据,最终找到了办法
使用puppeteer-core
const puppeteer = require("puppeteer-core");
const fs = require('fs')
const path = require('path')
const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time));
let browser,
page;
const getImg = async () => {
if (! browser) {
browser = await puppeteer.launch({
// headless: false,
// 这里的路径改成自己电脑上的路径
executablePath: "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
userDataDir: "C:\\Users\\81492\\AppData\\Local\\Google\\Chrome\\User Data",
defaultViewport: {
width: 1920,
height: 1080
},
args: [
// "--user-data-dir=C:\\Users\\81492\\AppData\\Local\\Google\\Chrome\\User Data",
// "--no-sandbox",
// "--disable-setuid-sandbox",
// "--start-maximized",
"--window-size=1920,1080",
],
// ignoreDefaultArgs: ["--disable-extensions"],
});
}
if (! page) {
page = await browser.newPage();
}
await page.goto("https://www.yuque.com/liulinboyi", {waitUntil: "networkidle2"});
await sleep(500);
const cover = await page.waitForSelector(".homepage-header");
await page.evaluate((el, value) => el.setAttribute("style", value), cover, "display: none");
fs.rmSync(path.resolve(__dirname, './demo.png'))
await page.screenshot({path: "demo.png", fullPage: true});
await browser.close();
};
(async function() {
await getImg()
})()
{
"name": "my-puppeteer",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node puppeteer.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"puppeteer-core": "^17.0.0"
}
}