Example: Screenshots
Save full-page screenshots or crop to a specific region. As of 0.3.0 the screenshot() method can write straight to disk and accept a bounding box.
Code
import asynciofrom pathlib import Path
from voidcrawl import BrowserPool, PoolConfig
OUTPUT_DIR = Path("output")
async def _capture() -> None: async with BrowserPool(PoolConfig()) as pool, pool.acquire() as tab: await tab.goto("https://qscrape.dev")
# 1. Raw bytes. Do whatever you want with them. png_bytes = await tab.screenshot() print(f"in-memory PNG: {len(png_bytes)} bytes")
# 2. Straight to disk. Returns the path string.
saved = await tab.screenshot(path=OUTPUT_DIR / "home.png") print(f"saved: {saved}")
# 3. Crop to a region (x, y, width, height in CSS pixels). hero = await tab.screenshot( path=OUTPUT_DIR / "hero.png", bbox=(0, 0, 1200, 400), ) print(f"cropped hero: {hero}")
def main() -> None: OUTPUT_DIR.mkdir(exist_ok=True) asyncio.run(_capture())
if __name__ == "__main__": main()Key Points
screenshot()returnsbytesby default; passpath=to write to disk and get back the path string.bbox=(x, y, w, h)crops the capture to a rectangle in CSS pixels, useful for evidence artefacts targeting a specific element.- The legacy
screenshot_png()method is preserved as a back-compat alias for the no-argument form. - For headful Docker mode, you can watch the page render in VNC while the screenshot is captured.
- See the Screenshot API guide for the full signature and HiDPI caveats.