使用puppeteer去获取网页上一个元素的内部文本
即为了获取下面组件中的“Painting Supplies”
<h1 id="searchTerm" class="searchQuery-UX2__searchQueryText" aria-label="Painting Supplies">Painting Supplies</h1>
根据指导文档是这样写的
const bodyHandle = await page.$('body');
const html = await page.evaluate(body => body.innerHTML, bodyHandle);
await bodyHandle.dispose();
然后根上述代码在实际中直接应用
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless:false,
defaultViewport:{
width:1500,
height:1080,
}
});
const page = await browser.newPage();
page.s
await page.goto('https://www.staples.com/paint/cat_CL140420');
await page.screenshot({path: 'example.png'});
const bodyHandle = await page.waitForSelector("h1[id='searchTerm']");
const html = await page.evaluate(body => {
return body.innerHTML;
}, bodyHandle);
console.log(html)
await browser.close();
})();
输出结果:
Painting Supplies