延續上一篇
const searchValue = await page.$eval('#search', el => el.value);
//link裡的屬性rel為preload
const preloadHref = await page.$eval('link[rel=preload]', el => el.href);
const html = await page.$eval('.main-container', e => e.outerHTML);
DOC範例
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);
await page.goto('https://www.google.com');
// other actions...
await browser.close();
});
拿到body
const bodyHandle = await page.$('body');
在第二個參數帶入
const html = await page.evaluate(body => body.innerHTML, bodyHandle);
棄用bodyHandle
await bodyHandle.dispose();
這邊稍微解釋一下ElementHandle與JSHandle
ElementHandle是負責處理DOM的element(HTML TAGS)
JSHandle是負責處理Javascript的物件
下一篇就可以把page結束掉