- console
用于监控浏览器的console事件。
'use strict';
const puppeteer = require('puppeteer');
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
page.on('console', (...args) => {
for (let i = 0; i < args.length; ++i)
console.log(`${i}: ${args[i]}`);
});
await page.goto('https://baidu.com');
browser.close();
});
- dialog
用于监控页面的JavaScript dialog事件。比如 alert, prompt, confirm or beforeunload。
'use strict';
const puppeteer = require('puppeteer');
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
page.on('dialog', dialog => {
console.log(dialog.message());
dialog.dismiss().then(()=>{
setTimeout(()=>{
browser.close();
},1);
})
});
page.evaluate(() => alert('alert dislog'));
});
- error
监控页面的崩溃事件。尝试了很多次居然不能响应到error事件。
- frameattached/framenavigated/framedetached
监控frame的attach/navigated/detached事件。
const puppeteer = require('puppeteer');
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
page.on('frameattached',(frame)=>{
console.log("new frameattached:");
})
page.on('framenavigated',(frame)=>{
console.log("framenavigated url: " + frame.url());
})
page.on('framedetached',(frame)=>{
console.log("framedetached url: " + frame.url());
})
await page.goto('http://ueditor.baidu.com/website/onlinedemo.html');
await browser.close();
});