-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
[Question] Screenshot clip vs elementHandle.screenShot #2854
Comments
Thank you for the issue!
So, Something like this should work for "area screenshot": // Calculate your clip rect. For a single element, that's usually the same as it's bounding box.
const clipRelativeToViewport = await someElementHandle.boundingBox();
// Translate clip to be relative to the page.
const clipRelativeToPage = {
width: clipRelativeToViewport.width,
height: clipRelativeToViewport.height,
x: clipRelativeToViewport + await page.evaluate(() => window.scrollX),
y: clipRelativeToViewport + await page.evaluate(() => window.scrollY),
};
// Take an area screenshot.
const areaScreenshot = await page.screenshot({ clip: clipRelativeToPage, fullPage: true });
Note that element screenshot does scroll the element into view, and I think performance does not suffer that much. More generally, taking screenshot of something not visible on the screen is hard (if not impossible) across multiple browsers, so you'll have to make the area visible on the screen anyway. |
Thanks for the answers, that makes sense. I didn't realize
If using the fullpage + clip approach do you foresee any problems then? Would you recommend forcing a scroll "just in case" (either by using |
Doing a |
Thanks, because the code dealing with this actually runs in the browser I have additional work to do to create a unique CSS selector and pass that to the node/playwright context so the elementHandle can be retrieved, but I think it might be worth it compared to dealing with conditional, cross-browser scrolling and viewport resize. I'll head in that direction. Thanks for your help! |
Hi there,
Thanks for playwright, we've built an in house visual regression testing tool and this has helped a lot!
I have a question regarding the behavior of
page.screenshot()
when using the clip option. Right now if the clip area is outside the viewport or bigger than the viewport then playwright will throw an error while taking the screenshot:I see in this issue that the alternate
elementHandle.screenshot
is supposed to handle scrolling the element, resizing the viewport and all that good stuff, and that the clip version of page.screenshot should follow the same behavior.My questions are:
page.screenshot
clip andelementHandle.screenshot
behave similarly as stated in the the issue? Right now the page one doesn't scroll or resize viewport.It would be sweet to be able to use the clip version without caring whether an element clip area is currently in the viewport and whether the viewport is currently big enough
We can work around limitations on our side by scrolling to the element before measuring its clip area but I'd like to avoid messing with the viewport on every screenshot since performance is paramount for us in the screenshot method
I can create some repro repo to expose the difference between the two screenshot functions if needed
The text was updated successfully, but these errors were encountered: