New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Screenshot an area of interest #306
Comments
Screenshot takes a |
Here's an example of taking a screenshot of a dom element, the logo on chromestatus.com: // adjustments for this page (so we hit the desktop breakpoint)
page.setViewport({width: 1000, height: 600, deviceScaleFactor: 2});
await page.goto('https://www.chromestatus.com/samples', {waitUntil: 'networkidle'});
async function screenshotDOMElement(selector, padding = 0) {
const rect = await page.evaluate(selector => {
const element = document.querySelector(selector);
const {x, y, width, height} = element.getBoundingClientRect();
return {left: x, top: y, width, height, id: element.id};
}, selector);
return await page.screenshot({
path: 'element.png',
clip: {
x: rect.left - padding,
y: rect.top - padding,
width: rect.width + padding * 2,
height: rect.height + padding * 2
}
});
}
await screenshotDOMElement('header aside', 16); |
@ebidel thanks for this example. It's getting there but one issue with this is that you have to know the |
Sure, just modify the snippet to take the coordinates directly :) |
@ebidel indeed ... const browser = await puppeteer.launch();
const page = await browser.newPage();
page.setViewport({ width: 1000, height: 600, deviceScaleFactor: 2 });
await page.goto('https://www.chromestatus.com/samples', { waitUntil: 'networkidle' });
await page.screenshot({
path: 'element.png',
clip: {x: 215, y: 0, width: 390, height: 50}
});
browser.close(); |
@ebidel why not add an |
0.12.0 has a new screenshot API for elements!
https://github.com/GoogleChrome/puppeteer/blob/v0.12.0/docs/api.md#elementhandlescreenshotoptions
…On Sun, Oct 15, 2017, 1:31 AM Wang Dàpéng ***@***.***> wrote:
@ebidel <https://github.com/ebidel> Could we make screenshotDOMElement an
option of screenshot api: selector?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#306 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAOigCvuOdKXSdtkHif7kRYpfxcLchZHks5sscLngaJpZM4O5eAU>
.
|
How to take screenshot for Xpath? |
This worked for me: /**
* Selecting the element by xpath
* page.$x() returns an array of ElementHandle.
* We are only interested in the first element
**/
const elByXpath = (await page.$x(req.param('elXpath')))[0]
const rect = await page.evaluate(element => {
if (!element)
return null;
const {x, y, width, height} = element.getBoundingClientRect();
return {left: x, top: y, width, height};
}, elByXpath); |
Not working! |
I'm using this it work well with selector outside frame or iframe but it can't find selector in frame or iframe. Can you fix it for me pls. |
It would be great if there would be an option to be able to screenshot an area of interest something like:
Then ...
... and you'd end up with selected area only e.g.
So we'd scroll to the point and draw an area or interest for screenshot. Any plans for this type of behaviour?
The text was updated successfully, but these errors were encountered: