Guides · Python · httpx
Wait for a specific element before capturing in Python with httpx
A fixed delay is a guess: too short and you capture the spinner, too long and every render pays for the worst case. wait_for_selector replaces the guess with a condition — pass a CSS selector and the render proceeds the moment a matching element is visible. The dashboard's chart, the map's tiles, the price table: name the thing you are actually waiting for, and wait for that.
Below is a complete, runnable Python program using httpx (pip install httpx). It reads your API key from the SNAPDOK_KEY environment variable — free keys take about thirty seconds and need no card.
import os
import httpx
resp = httpx.post(
"https://snapdok.io/v1/render",
headers={"Authorization": "Bearer " + os.environ["SNAPDOK_KEY"]},
json={
"url": "https://your-app.com/report/42",
"format": "png",
"wait_for_selector": "#chart-ready",
"wait_until": "networkidle",
},
timeout=90,
)
resp.raise_for_status()
with open("ready.png", "wb") as f:
f.write(resp.content)
print(resp.headers.get("X-Cache"), "# the settled render is cached like any other")
Selector choice is the whole game. Pick an element that appears last in your page's load sequence — a #chart-ready marker your own code adds after drawing, the final list item, the element a skeleton placeholder is replaced by. An element that exists in the initial HTML matches immediately and waits for nothing; visibility is required precisely so a pre-rendered-but-hidden node does not count as ready.
The failure mode is the feature: if the element never becomes visible within timeout, the render fails with 504 SELECTOR_TIMEOUT — not metered, and unambiguous in your logs — instead of silently shipping a half-rendered image the way an expired delay does. The sequencing composes with the other waits: navigation finishes (wait_until), then delay runs, then the selector wait begins. It works on every format, pdf_forms included.
Same endpoint, one more trick: if the page you are rendering has a form on it, adding "pdf_forms": true to a PDF render brings it back with real, fillable AcroForm fields — a PDF people can type into, not a picture of one. How that works.
Notes for httpx
httpx looks like requests but differs where it bites: it ships a 5-second default timeout, which a render that drives a real browser will blow through on heavier pages — hence the explicit timeout=90. raise_for_status() exists and behaves the same. If you are on asyncio already, the same call works with httpx.AsyncClient and await client.post(...) — the request body and headers are identical.
Response headers worth reading
| Header | Meaning |
|---|---|
X-Cache | HIT when an identical request rendered within 24h. |
X-RateLimit-Remaining | Requests left in the current second. |
Full parameter reference: the docs. Hard numbers on caps and timeouts: limits.
Related guides
Same task, other stacks
- Wait for a specific element before capturing in Python with requests
- Wait for a specific element before capturing in Node.js with built-in fetch
- Wait for a specific element before capturing in Node.js with axios
- Wait for a specific element before capturing in PHP with cURL extension
- Wait for a specific element before capturing in Ruby with Net::HTTP
More with Python + httpx
- Convert a URL to PDF
- Take a full-page screenshot
- Capture very tall pages as numbered tiles
- Take a retina-quality (2x) screenshot
- Take a JPEG screenshot at a custom viewport size
- Use the 24-hour render cache to cut costs
- Take a screenshot of a URL
- Capture a JavaScript-heavy page after it settles
- Add page numbers and headers to a PDF