截图
¥Screenshots
要捕获屏幕截图,请使用 Page.screenshot()
。
¥For capturing screenshots use Page.screenshot()
.
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://news.ycombinator.com', {
waitUntil: 'networkidle2',
});
await page.screenshot({
path: 'hn.png',
});
await browser.close();
你还可以使用 ElementHandle.screenshot()
捕获特定元素的屏幕截图:
¥You can also capture a screenshot of a specific element using ElementHandle.screenshot()
:
const fileElement = await page.waitForSelector('div');
await fileElement.screenshot({
path: 'div.png',
});
默认情况下,如果元素处于隐藏状态,ElementHandle.screenshot()
会尝试将其滚动到视图中。
¥By default, ElementHandle.screenshot()
tries to scroll the element into view
if it is hidden.