TechBuzzPro

Fix Playwright Screenshots Cut Off at Bottom

· 6 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Playwright's full-page screenshots sometimes come out shorter than expected, with the bottom of the page missing. This usually happens because the page has dynamic content, lazy-loaded images, or a fixed viewport that doesn't match the actual page height. This guide walks you through reliable ways to capture the complete page, including the areas that commonly get cut off.

What You'll Need

  • A Node.js project with Playwright installed (npm init playwright@latest or npm install @playwright/test)
  • Basic familiarity with writing and running Playwright tests
  • Access to the page you're trying to screenshot (local or remote)

Step-by-Step

  1. Use the correct screenshot method. For a full-page capture, always call page.screenshot({ fullPage: true }). Without this option, Playwright only captures the visible viewport, which is the most common cause of a cut-off bottom.
  2. Wait for the page to settle before shooting. Dynamic content can change the page height after the initial load. Use await page.waitForLoadState('networkidle') or wait for specific elements that affect layout (e.g., await page.waitForSelector('.footer')). This ensures the height is final when the screenshot is taken.
  3. Scroll to the bottom first. Some pages load content only when you scroll (lazy loading). Before the screenshot, scroll to the bottom, then back to the top, so all images and sections are rendered. Use await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight)), wait a moment, then scroll back up.
  4. Set a large viewport if the page is very tall. Playwright's default viewport (1280x720) can cause the screenshot to be scaled or clipped on extremely long pages. Increase the viewport height in the test's use options, for example viewport: { width: 1280, height: 2000 }. This gives the rendering engine more room to capture the full height.
  5. Check for fixed or sticky elements. A fixed header or footer can overlap content and cause the bottom to appear cut off in the screenshot. Temporarily hide them with await page.addStyleTag({ content: 'header, footer { position: static !important; }' }) before taking the screenshot, then remove the style after.
  6. Use clip as a fallback for stubborn pages. If fullPage still fails, manually compute the page height and capture that region. Get the height with const height = await page.evaluate(() => document.body.scrollHeight), then call page.screenshot({ path: 'full.png', clip: { x: 0, y: 0, width: 1280, height } }). This forces the capture area to match the actual content height.
  7. Save to a file with an absolute path. Ensure the output path is correct and the directory exists. Use path.join(__dirname, 'screenshots', 'page.png') and create the folder with fs.mkdirSync if needed. A wrong path can cause the screenshot to be saved elsewhere or fail silently.
  8. Run the test in headed mode to debug. Use npx playwright test --headed to see what the browser actually displays. This helps you spot if the page is scrolling, if elements are overlapping, or if the viewport is too small.

Common Mistakes

  • Forgetting fullPage: true. This is the number one cause. Without it, you only get the viewport, and the bottom is always cut off.
  • Not waiting for lazy-loaded content. If images or sections load after scrolling, the screenshot may capture a blank area instead of the actual content, making it look cut off.
  • Using a viewport that is too small. A very tall page might exceed the maximum renderable height in some browsers, causing clipping. Increase the viewport height.
  • Assuming the page height is static. Many modern sites use dynamic layouts, so the height at the moment of the screenshot may not be the final height. Always wait for network idle or specific elements.

Tips

  • Take screenshots after all network requests have completed. Use page.waitForLoadState('networkidle') to avoid missing elements that load late.
  • If you're testing a single-page app, wait for the route to be fully rendered. Use await page.waitForSelector('some-unique-element') to ensure the content you want is present.
  • For consistent results across runs, set a fixed viewport size in your Playwright config. This avoids variability due to different default window sizes.
  • Consider using page.setViewportSize() dynamically if you know the page height beforehand. This can be more reliable than relying on fullPage.
  • If the page has infinite scroll, you may need to scroll multiple times to load all content. Write a loop that scrolls until the height stops increasing.

Troubleshooting

The screenshot is still cut off after following all steps. First, verify that the page actually has the content you expect at the bottom. Open the page manually and scroll down. If the content is there, try the clip method with an explicit height. If that fails, the page might be using a canvas or WebGL that doesn't render in screenshots. In that case, you may need to use a different approach, like capturing the canvas separately.

The screenshot is blank at the bottom. This often means the content is lazy-loaded and hasn't been triggered. Ensure you scroll to the bottom and wait for a few seconds before taking the screenshot. You can also use page.mouse.wheel(0, 10000) to trigger scroll events.

The screenshot is extremely tall and distorted. Some pages have elements that stretch the body height incorrectly (e.g., a fixed background). Try setting page.setViewportSize({ width: 1280, height: 800 }) and then capture with fullPage: true. If that doesn't work, use the clip method with a manually calculated height.

The screenshot works in headed mode but not headless. Headless mode has a different default viewport and may handle rendering differently. Set the viewport explicitly in your config or in the test to match what you see in headed mode.

Frequently Asked Questions

Why does my full-page screenshot only capture the visible area?

This happens when you forget to pass fullPage: true to the screenshot() method. Without it, Playwright captures only the current viewport. Double-check your code to ensure the option is present.

How do I take a screenshot of a page that has infinite scroll?

You need to scroll repeatedly until the page height stops increasing. Use a loop that scrolls to the bottom, waits for new content to load, and repeats until the height stabilizes. Then take the screenshot.

Can I set a custom viewport for screenshots?

Yes, use page.setViewportSize({ width: 1920, height: 1080 }) before taking the screenshot. This is especially useful for desktop layouts that might otherwise be cut off.

Why is my screenshot blurry or low resolution?

Playwright captures at the device pixel ratio by default. If you need a higher resolution, set deviceScaleFactor in the browser context options. For example, context = await browser.newContext({ deviceScaleFactor: 2 }).

How do I capture only a specific element instead of the whole page?

Use locator.screenshot() on the element you want. For example, await page.locator('.footer').screenshot({ path: 'footer.png' }). This captures just that element, even if it's partially below the fold.

What if the page uses a CSS grid or flexbox that changes height dynamically?

Wait for the layout to settle by using page.waitForTimeout(500) or better, wait for a specific element that indicates the layout is complete. Then take the screenshot. If necessary, use the clip method with the computed height.