await page.waitFor(2000);
const puppeteer = require('puppeteer'); puppeteer.launch({headless: false, slowMo: 250}).then(async browser => { const page = await browser.newPage(); await page.setRequestInterception(true); page.on('console', msg => console.log('PAGE LOG:', msg.text())); page.on('request', interceptedRequest => { if (interceptedRequest.url().endsWith('.png') || interceptedRequest.url().endsWith('.jpg')) interceptedRequest.abort(); else interceptedRequest.continue(); }); await page.goto('https://open.toutiao.com');// await browser.close();});
await page.setViewport({ width: 1920, height: 1080});
await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36');
const puppeteer = require('puppeteer');const devices = require('puppeteer/DeviceDescriptors');const iPhone = devices['iPhone 6'];
puppeteer.launch().then(async browser => { const page = await browser.newPage(); await page.emulate(iPhone); // emulate的配置有Viewport,UserAgent等等。以前的setUserAgent等方法是它的語法糖。 await page.goto('https://www.google.com'); // other actions... await browser.close();});
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.tracing.start({path: 'trace.json'}) await page.goto('https://open.toutiao.com') await page.tracing.stop() const metrics = await page.metrics() console.log(metrics) await browser.close();})();// output{ Timestamp: 27888.820538, Documents: 2, Frames: 1, JSEventListeners: 58, Nodes: 171, LayoutCount: 20, RecalcStyleCount: 26, LayoutDuration: 0.042335, RecalcStyleDuration: 0.010091, ScriptDuration: 0.124838, TaskDuration: 0.000039, JSHeapUsedSize: 6388448, JSHeapTotalSize: 10334208 }