Cookbook

Copy-paste recipes for the things people build most. Every snippet uses the real API; adapt the selectors and URLs to your target.

Log in with human-like input

login.py
from capium import launch_context

browser, ctx, page = launch_context(
    seed=200123, humanize=True,
    proxy="http://user:pass@host:port", geoip=True,
    url="https://example.com/login",
)
page.human_type("#email", "[email protected]")
page.human_type("#password", "hunter2")
page.human_click("button[type=submit]")
page.wait_for_url("**/dashboard")
browser.close()

Scrape a paginated list

paginate.py
from capium import launch_context

browser, ctx, page = launch_context(seed=200123, url="https://example.com/list")
rows = []
while True:
    for el in page.query_selector_all(".item"):
        rows.append(el.inner_text())
    nxt = page.query_selector("a[rel=next]")
    if not nxt:
        break
    nxt.click()
    page.wait_for_load_state("domcontentloaded")
browser.close()
print(len(rows), "items")

Persist a session between runs

A persistent context keeps cookies and login, so you authenticate once.

persist.py
from capium import launch_persistent_context

# Log in once; the profile keeps the session for next time.
ctx = launch_persistent_context("~/profiles/acct1", seed=200123,
                                proxy="http://user:pass@host:port", geoip=True)
page = ctx.pages[0] if ctx.pages else ctx.new_page()
page.goto("https://example.com/dashboard")   # already logged in on run #2
ctx.close()

Isolate multiple accounts

Keep each identity on its own seed, profile and sticky proxy so they never bleed together.

multi_account.py
from capium import launch_persistent_context

# One seed + one profile + one sticky proxy PER account = clean isolation.
ACCOUNTS = [
    ("acct1", 200123, "http://u1:p1@host:port"),
    ("acct2", 200456, "http://u2:p2@host:port"),
]
for name, seed, proxy in ACCOUNTS:
    ctx = launch_persistent_context(f"~/profiles/{name}", seed=seed,
                                    proxy=proxy, geoip=True, platform="linux")
    page = ctx.new_page()
    page.goto("https://example.com")
    ctx.close()

Screenshots & PDF

capture.py
from capium import launch_context

browser, ctx, page = launch_context(seed=200123, url="https://example.com")
page.screenshot(path="page.png", full_page=True)
page.pdf(path="page.pdf")            # Chromium PDF (headed via Xvfb on servers)
browser.close()

For concurrency and proxy alignment behind these recipes, see Async & persistent sessions and Proxies & geo.