Files & Downloads
Download, upload, unzip, browse directories and work with PDFs.
All file paths are relative to the run's downloads directory unless stated otherwise. Use $getDownloadsPathFile() to get absolute paths.
Creating files
$writeFile(fileName, content, options?)
Write text or structured content to the run's downloads directory and return the absolute path.
const path = await $writeFile('result.json', { status: 'complete' }, {
format: 'json',
output: true,
overwrite: true,
});| Option | Default | Description |
|---|---|---|
format | text | Output format: text, json, yaml, csv, toml, or xml. |
output | true | Include the file in run artifacts. |
overwrite | true | Replace an existing file. |
structuredSpacing | 2 | JSON indentation, clamped from 0 to 10. |
Downloading
$download(url, filename?, options?)
Download a file via HTTP using the page's cookies and user-agent. Works regardless of whether the browser is local or remote.
await $download('https://app.example.com/report.csv', 'report.csv');
await $download(linkUrl, 'invoice.pdf', { timeout: 60000 });| Option | Default | Description |
|---|---|---|
output | true | Include in run artifacts. |
timeout | 100000 | Timeout (ms). |
If filename is omitted, the name is inferred from the URL.
$downloadFromBrowser(url, filename?, options?)
Download via the browser session (navigates to the URL and triggers a download click). Useful for files that require browser context or session cookies that can't be extracted.
Returns the absolute path to the downloaded file.
const filePath = await $downloadFromBrowser(
'https://app.example.com/export',
'export.xlsx'
);| Option | Default | Description |
|---|---|---|
output | true | Include in run artifacts. |
timeout | 100000 | Timeout (ms). |
$waitForFile(dest?, options?)
Wait for a browser-initiated download to complete.
await $clickElement('#export-btn');
await $waitForFile('export.csv');| Option | Default | Description |
|---|---|---|
output | true | Include in run artifacts. |
timeout | 100000 | Timeout (ms). |
overrideExtension | false | Append the original file extension to dest. |
noThrow | false | Return false instead of throwing on timeout. |
Uploading
$upload(selectorOrHandle, filename, options?)
Upload a file from the downloads directory or Media Library to an <input type="file"> element. Accepts a CSS selector or ElementHandle.
await $upload('#file-input', 'document.pdf');
// With an ElementHandle
const input = await $page.$('input[type="file"]');
await $upload(input, 'photo.jpg');filename is resolved inside the run downloads directory; paths that escape it are rejected. A Media Library ID in media_XXXXXXXXXXXX format is also accepted and staged for upload.
Options include timeout (default 30000), continueOnError, visibleOnly, and index. The helper returns true, or null when the target is not found and continueOnError is enabled.
Archives
$unzipFile(zipPath, extractPath, options?)
Extract a ZIP archive from the downloads directory into a subdirectory. An existing extraction directory is replaced. The archive is removed by default. Returns the list of extracted filenames.
const files = await $unzipFile('archive.zip', 'extracted');
// files = ['file1.csv', 'file2.csv']| Option | Default | Description |
|---|---|---|
store | true | Keep extracted files after the run ends. Set false to auto-delete. |
keepArchive | false | Keep the ZIP file after extraction. |
Directory operations
$scanDirectory(subPath, nameLike?)
List files in a directory, optionally filtering by name substring. Excludes .crdownload temp files.
const allFiles = await $scanDirectory('/absolute/path/to/dir');
const csvFiles = await $scanDirectory('/absolute/path', '.csv');$scanDownloadsDirectory(subPath, nameLike?)
Same as $scanDirectory but relative to the downloads directory.
const files = await $scanDownloadsDirectory('extracted', '.csv');$moveDownloadedFile(source, destination)
Move a file to another path inside the downloads directory.
$moveDownloadedFile('temp-name.csv', 'final-report.csv');$getDownloadsPathFile(file)
Get the absolute path to a file in the downloads directory.
const absPath = $getDownloadsPathFile('report.csv');PDF
$pdfSearch(filepath, text)
Search for a text string in a PDF file. Matching is case-insensitive. The path is resolved inside the run downloads directory.
const result = await $pdfSearch('document.pdf', 'Invoice Total');
// { found: true, count: 2, pages: [1, 3], totalPages: 5 }Returns:
| Property | Type | Description |
|---|---|---|
found | boolean | Whether any match was found. |
count | number | Total number of occurrences. |
pages | number[] | Page numbers containing the match (1-indexed). |
totalPages | number | Total number of pages in the PDF. |
The PDF must contain searchable text. Scanned image-only pages require OCR before they can be searched.
$pdfGetText(filepath)
Extract all text content from a PDF file. The path is resolved inside the run downloads directory.
const result = await $pdfGetText('document.pdf');
console.log(result.text);
result.pages.forEach(p => {
console.log(`Page ${p.page}: ${p.text.substring(0, 100)}...`);
});Returns:
| Property | Type | Description |
|---|---|---|
text | string | Full text content of the PDF. |
pages | Array<{ page, text }> | Per-page text (1-indexed, non-empty pages only). |
totalPages | number | Total number of pages. |
