Skip to content

Rendering & Interaction

With the plugin installed, every PAM Crawl crawler can drive a browser. This guide walks through the browser features in the order you usually need them: waiting for content, running scripts, scrolling, capturing output, inspecting traffic and keeping a logged-in tab. All examples use the regular Crawl builder; see PAM Crawl Browser for installation.

Any browser-only method (js(), waitFor(), screenshot() and so on) renders the page even without ->browser().

Single-page apps render after the HTML arrives. By default the plugin waits for domcontentloaded and then for a short settle step, until the network and the DOM are quiet, which catches most apps without any configuration. When you know what to wait for, say so:

crawl.php
use Pam\Crawl\Crawl;
$page = Crawl::url('https://quotes.toscrape.com/js-delayed/')
->waitFor('css:div.quote', 15) // a CSS selector, up to 15 s
->get();
$page = Crawl::url('https://app.example.com/report')
->waitFor('js:() => window.reportReady === true')
->waitUntil('networkidle') // domcontentloaded | load | networkidle
->get();

waitFor() accepts css:selector, js:() => condition or a bare CSS selector, with Crawl4AI’s wait_for semantics. Polling works even on pages with a strict Content Security Policy.

js() runs code in the page after load. Each snippet may return a value; results land in $page->jsResults, in order:

$page = Crawl::url('https://example.com/products')
->js('document.querySelector("#load-more")?.click()')
->waitFor('css:.product:nth-child(40)', 10)
->js('return document.title', 'return document.querySelectorAll(".product").length')
->get();
[$_, $title, $count] = $page->jsResults;

For multi-step automation, write Crawl4AI’s C4A-Script instead of JavaScript. It is compiled to JavaScript and appended to the page scripts:

$page = Crawl::url('https://quotes.toscrape.com/login')
->script(<<<'C4A'
WAIT `#username` 10
SET `#username` "alice"
SET `#password` "secret"
CLICK `input[type=submit]`
C4A)
->get();

A script that does not compile throws Pam\Crawl\Script\CompileError with the line, column and suggestions. On the command line, use --c4a login.c4a. See JavaScript Pages for the language.

// Infinite feeds: scroll to the bottom until nothing new loads
$page = Crawl::url('https://quotes.toscrape.com/scroll')
->scrollFullPage(delay: 0.2, maxSteps: 20)
->get();
// Virtualized lists that recycle DOM rows: rows are merged uniquely
$page = Crawl::url('https://app.example.com/feed')
->virtualScroll('#feed', count: 30)
->get();

virtualScroll() also takes $by ('container_height' by default) and $wait (seconds between scrolls, default 0.5).

$page = Crawl::url('https://news.example.com/')
->magic() // stealth, user simulation, overlay and consent removal
->blockResources('image', 'font', 'media')
->get();

magic() hides common automation fingerprints, simulates a user, and removes overlays and consent banners. Set the pieces individually through renderOptions():

use Pam\Crawl\Render\RenderOptions;
Crawl::url($url)->renderOptions(fn (RenderOptions $o) => $o->with([
'removeConsentPopups' => true,
'removeOverlays' => true,
'processIframes' => true, // inline same-origin and cross-origin iframes
'flattenShadowDom' => true, // closed shadow roots included
]));

blockResources() accepts image, media, font, stylesheet, script, xhr and other.

Crawl::url($url)
->viewport(390, 844, scale: 3, mobile: true)
->userAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) ...')
->renderOptions(fn ($o) => $o->with(['locale' => 'pt-BR', 'timezone' => 'America/Sao_Paulo']));

The browser uses the same identity as HTTP crawling: user agent, headers, cookies, the first proxy and TLS settings carry over from the crawler. Set a location with the geolocation render option, for example ['latitude' => -23.55, 'longitude' => -46.63, 'accuracy' => 10] (Chrome only; the permission is granted for the page’s origin).

$page = Crawl::url('https://example.com/')
->screenshot() // full page; screenshot(fullPage: false) for the viewport
->capturePdf()
->captureMhtml()
->get();
file_put_contents('page.png', $page->screenshot);
file_put_contents('page.pdf', $page->pdfBytes);
file_put_contents('page.mhtml', $page->mhtml);

Very tall pages (over 10,000 pixels by default) are captured in chunks and stitched together; install the gd extension to get one PNG, otherwise the chunks are in $page->response->artifacts['screenshots']. Captures always run in Chrome, even when Lightpanda rendered the page.

$page = Crawl::url('https://quotes.toscrape.com/scroll')
->captureNetwork()
->captureConsole()
->renderOptions(fn ($o) => $o->with(['extra' => $o->extra + ['networkBodies' => true]]))
->get();
foreach ($page->network as $entry) {
if (($entry['event_type'] ?? '') === 'response' && str_contains($entry['url'], '/api/')) {
echo $entry['status'], ' ', $entry['url'], "\n", $entry['body'] ?? '', "\n";
}
}
foreach ($page->console as $message) {
echo "[{$message['type']}] {$message['text']}\n";
}

$page->network lists requests, responses, failures and timing. Response bodies are captured only with the networkBodies option; see Engines for their limits.

$page = Crawl::url('https://example.com/report')
->downloads(__DIR__.'/storage/downloads')
->js('document.querySelector("a.export").click()')
->get();
$page->response->artifacts['downloads']; // what the browser saved

Without downloads(), browser downloads are cancelled. Downloads need Chrome.

tab() keeps one browser tab across calls, like Crawl4AI’s session_id. Log in once, then crawl as that user in the same tab:

crawl.php
use Pam\Crawl\Browser\BrowserRenderer;
use Pam\Crawl\Crawl;
$renderer = BrowserRenderer::chrome();
try {
Crawl::url('https://quotes.toscrape.com/login')
->browser($renderer)
->tab('alice')
->script(<<<'C4A'
WAIT `#username` 10
SET `#username` "alice"
SET `#password` "secret"
CLICK `input[type=submit]`
C4A)
->get();
$page = Crawl::url('https://quotes.toscrape.com/page/2/')
->browser($renderer)
->tab('alice')
->get();
$renderer->killSession('alice'); // close the tab
} finally {
$renderer->close(); // stops exactly the browsers this renderer started
}

When you do not pass a renderer, tab() sessions share one renderer per process. In a persistent PAM worker it stays warm between requests; it closes at shutdown or when you call Crawler::closeTabs().

For logins that should survive restarts, use a browser profile instead: create it once with pam crawl profiles create NAME (see Browser CLI), then crawl with --profile NAME or the userDataDir option.

Create a renderer yourself to control the browser, share it between crawls and close it explicitly:

use Pam\Crawl\Browser\BrowserConfig;
use Pam\Crawl\Browser\BrowserRenderer;
use Pam\Crawl\Crawl;
$renderer = BrowserRenderer::chrome(new BrowserConfig(tabs: 4, userDataDir: __DIR__.'/profile'));
try {
foreach (Crawl::site('https://shop.example.com/')->depth(2)->browser($renderer) as $page) {
// ...
}
} finally {
$renderer->close();
}

Other constructors: BrowserRenderer::lightpanda(), BrowserRenderer::connect($endpoint) for a remote browser, and BrowserRenderer::fromOptions($renderOptions), which is what the crawler uses when you do not pass one.


This product includes software developed by UncleCode (https://x.com/unclecode) as part of the Crawl4AI project (https://github.com/unclecode/crawl4ai).