[Go to site: main page, start]

Skip to content

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.

js
const path = await $writeFile('result.json', { status: 'complete' }, {
  format: 'json',
  output: true,
  overwrite: true,
});
OptionDefaultDescription
formattextOutput format: text, json, yaml, csv, toml, or xml.
outputtrueInclude the file in run artifacts.
overwritetrueReplace an existing file.
structuredSpacing2JSON 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.

js
await $download('https://app.example.com/report.csv', 'report.csv');
await $download(linkUrl, 'invoice.pdf', { timeout: 60000 });
OptionDefaultDescription
outputtrueInclude in run artifacts.
timeout100000Timeout (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.

js
const filePath = await $downloadFromBrowser(
  'https://app.example.com/export',
  'export.xlsx'
);
OptionDefaultDescription
outputtrueInclude in run artifacts.
timeout100000Timeout (ms).

$waitForFile(dest?, options?) ​

Wait for a browser-initiated download to complete.

js
await $clickElement('#export-btn');
await $waitForFile('export.csv');
OptionDefaultDescription
outputtrueInclude in run artifacts.
timeout100000Timeout (ms).
overrideExtensionfalseAppend the original file extension to dest.
noThrowfalseReturn 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.

js
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.

js
const files = await $unzipFile('archive.zip', 'extracted');
// files = ['file1.csv', 'file2.csv']
OptionDefaultDescription
storetrueKeep extracted files after the run ends. Set false to auto-delete.
keepArchivefalseKeep the ZIP file after extraction.

Directory operations ​

$scanDirectory(subPath, nameLike?) ​

List files in a directory, optionally filtering by name substring. Excludes .crdownload temp files.

js
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.

js
const files = await $scanDownloadsDirectory('extracted', '.csv');

$moveDownloadedFile(source, destination) ​

Move a file to another path inside the downloads directory.

js
$moveDownloadedFile('temp-name.csv', 'final-report.csv');

$getDownloadsPathFile(file) ​

Get the absolute path to a file in the downloads directory.

js
const absPath = $getDownloadsPathFile('report.csv');

PDF ​

Search for a text string in a PDF file. Matching is case-insensitive. The path is resolved inside the run downloads directory.

js
const result = await $pdfSearch('document.pdf', 'Invoice Total');
// { found: true, count: 2, pages: [1, 3], totalPages: 5 }

Returns:

PropertyTypeDescription
foundbooleanWhether any match was found.
countnumberTotal number of occurrences.
pagesnumber[]Page numbers containing the match (1-indexed).
totalPagesnumberTotal 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.

js
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:

PropertyTypeDescription
textstringFull text content of the PDF.
pagesArray<{ page, text }>Per-page text (1-indexed, non-empty pages only).
totalPagesnumberTotal number of pages.