-
Notifications
You must be signed in to change notification settings - Fork 1
/
puppeteer_test.js
58 lines (46 loc) · 1.66 KB
/
puppeteer_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
puppeteer.use(StealthPlugin());
// Fix Error: An `executablePath` or `channel` must be specified for `puppeteer-core`
const {executablePath} = require('puppeteer');
// const stealth = StealthPlugin();
// stealth.enabledEvasions.delete('user-agent-override');
// stealth.enabledEvasions.delete('sourceurl');
// puppeteer.use(stealth);
const url = "https://nodejs.org/en/";
const CaptureElement = "#logo > img";
const os = require("os");
const userHomeDir = os.homedir();
const ScreenshotDir = `${userHomeDir}/puppeteer/screenshots`;
const UserDataDir = `${userHomeDir}/puppeteer/userdata`;
const OutputFileFullPage = `${ScreenshotDir}/screenshot_fullpage.png`;
const OutputFileElement = `${ScreenshotDir}/screenshot_element.png`;
const PuppeteerScreenshotTest = async () => {
const browser = await puppeteer.launch({
headless: true,
// defaultViewport: {
// width: 800,
// height: 600
// },
dumpio: true,
userDataDir: `${UserDataDir}`,
executablePath: executablePath(),
});
const page = await browser.newPage();
// await page.setViewport({width: 1024, height: 768});
await page.goto(url);
await page.waitForSelector(CaptureElement);
// fullpage
// await page.screenshot({
// path: `${OutputFileFullPage}`,
// fullPage: true,
// });
// element
const pageElement = await page.$(CaptureElement);
await pageElement.screenshot({
path: `${OutputFileElement}`,
});
await page.close();
await browser.close();
}
PuppeteerScreenshotTest();