Capture Full Browser Window Screenshots with Playwright
In this article
Capturing a full browser window or a full-page screenshot in Playwright allows you to document the entire length of a webpage, including content that requires scrolling to see. By using the fullPage: true option within the screenshot() method, Playwright automatically calculates the total height of the rendered DOM and captures every pixel from the top header to the bottom footer in a single image file.
What You'll Need
- Node.js installed on your local machine.
- A project initialized with
npm init. - Playwright installed via
npm install playwrightornpm init playwright@latest. - A code editor such as Visual Studio Code.
Step-by-Step
- Import Playwright: Start by importing the necessary modules. If you are using JavaScript, require
chromium(or firefox/webkit) from the playwright package. - Launch the Browser: Initialize the browser instance. For debugging, you can set
headless: falseto see the browser window, but for automated scripts,headless: trueis the default and most efficient. - Create a New Page: Open a new browser context and page. This ensures a clean session without cached data or cookies from previous runs.
- Navigate to the URL: Use
page.goto('your-url-here')to load the target website. It is highly recommended to usewaitUntil: 'networkidle'to ensure all images, scripts, and styles are fully loaded before the capture begins. - Set the Viewport (Optional): If you need the screenshot to represent a specific device size (like a 1080p monitor), use
page.setViewportSize({ width: 1920, height: 1080 }). Note thatfullPage: truewill ignore the height setting and expand to the full content length. - Execute the Full Page Screenshot: Call the screenshot method with the
fullPageproperty set totrue. Example:await page.screenshot({ path: 'fullpage.png', fullPage: true });. - Close the Browser: Always call
await browser.close()to release system resources and prevent orphaned browser processes.
Common Mistakes
- Ignoring Lazy Loading: Many modern websites only load images as you scroll down. A standard
fullPage: truecommand may result in blank spaces or placeholders for images that haven't been triggered to load. - Capturing Too Early: Taking a screenshot immediately after
page.goto()often captures a loading spinner or a partially rendered page. Always wait for the network to be idle or for a specific element to be visible. - Confusing Viewport with Full Page: Some users attempt to set a massive viewport height (e.g., 10,000px) to get a full page. This is inefficient and can crash the browser; using
fullPage: trueis the native and correct way to handle this. - Overlooking Sticky Elements: Elements with
position: fixed(like navigation bars) can sometimes appear multiple times or create visual glitches in a stitched full-page screenshot.
Tips
- Handle Lazy Loading with a Scroll Script: To ensure all images are captured, run a small JavaScript snippet in the browser using
page.evaluate()that scrolls to the bottom of the page and back up before taking the screenshot. - Use PNG for Quality, JPEG for Size: Playwright defaults to PNG. If the page is extremely long, the file size can become massive. Use the
type: 'jpeg'andquality: 80options to reduce file size. - Hide Elements: If there are pop-ups, cookie banners, or chat widgets blocking the content, use
page.addStyleTag()to inject CSS that sets those elements todisplay: none !important;. - Consistent Viewports: Always define a explicit width in your viewport settings to ensure the layout (desktop vs. mobile) is consistent across different environments.
Troubleshooting
- Screenshot is Cut Off: If the image ends prematurely, check if the page has a container with
overflow: hiddenorheight: 100vhon the body/html tags. Playwright relies on the document height; if the CSS restricts the body height, the screenshot will be limited. - Images are Missing: This is almost always due to lazy loading. Implement a scroll-to-bottom loop:
await page.evaluate(async () => { window.scrollTo(0, document.body.scrollHeight); });and then wait a few seconds for assets to load. - Memory Errors: Extremely long pages (e.g., infinite scroll feeds) can consume all available RAM during the stitching process. In these cases, consider taking multiple smaller screenshots of specific sections instead of one giant file.
- Blurry Text: Ensure you are not using a scaled viewport. Stick to standard integer dimensions for the width to avoid sub-pixel rendering issues.
Frequently Asked Questions
Does fullPage: true work on mobile emulators?
Yes, it works. When you use a mobile device descriptor in Playwright, fullPage: true will capture the entire vertical length of the mobile-rendered page.
Can I capture only a specific element instead of the whole page?
Yes. Instead of page.screenshot(), you can locate an element using page.locator('.selector') and call .screenshot() on that specific locator.
What is the difference between a viewport screenshot and a full-page screenshot?
A viewport screenshot captures only what is currently visible in the browser window. A full-page screenshot captures the entire scrollable area of the document.
Can I save the screenshot as a PDF instead?
Yes, Playwright provides a page.pdf() method. Note that PDF generation is currently only supported in Chromium-based browsers.
How do I deal with cookie banners that block the view?
The best approach is to use page.click('text=Accept All') to dismiss the banner or use page.evaluate() to remove the banner element from the DOM before capturing the image.