crawlee icon indicating copy to clipboard operation
crawlee copied to clipboard

PlaywrightCrawler: expose Download object in errorHandler / add dedicated downloadHandler hook

Open nikitachapovskii-dev opened this issue 7 months ago • 5 comments

Feature

When page.goto throws "Download is starting", Playwright has already captured the file in the browser — the Download object exists and is accessible. However, PlaywrightCrawler does not expose it to user-land. The errorHandler receives only the Error, with no way to access the download that caused it.

The canonical workaround is to re-enqueue the URL to a separate HTTP crawler (FileDownload / raw HTTP client). This worked historically, but breaks on any site that requires an authenticated browser session to serve the file — the raw HTTP client has no session cookies and receives an HTML auth-challenge instead of the file bytes.

How this was discovered

We were crawling Eur-Lex (EU legal documents portal). https://github.com/apify/store-website-content-crawler/issues/713 The site changed its behavior: PDF URLs that previously served files to unauthenticated HTTP requests now require a browser session. After the change:

  • PlaywrightCrawler still correctly detected downloads ("Download is starting")
  • Re-downloading the same URL via ImpitHttpClient (no browser session) returned text/html so all file downloads silently failed

I tried to test it with couple of workarounds on WCC side and managedto intercept the download event in a preNavigationHook, before page.goto runs, https://console.apify.com/view/runs/sSNeHuQi1e3GgFJxL this works:

  const pendingDownloads = new Map<string, Download>();                                                                                                                                                                                                                                                                                                                                               
                                                            
  // preNavigationHook:                                                                                                                                                                                                                                                                                                                                                                               
  context.page.once('download', (download) => {
      pendingDownloads.set(context.request.uniqueKey, download);                                                                                                                                                                                                                                                                                                                                      
  });                                                                                                                                                                                                                                                                                                                                                                                                 
   
  // errorHandler:                                                                                                                                                                                                                                                                                                                                                                                    
  if (error.message.includes('Download is starting')) {     
      const download = pendingDownloads.get(request.uniqueKey);
      pendingDownloads.delete(request.uniqueKey);                                                                                                                                                                                                                                                                                                                                                     
   
      if (download) {                                                                                                                                                                                                                                                                                                                                                                                 
          const filePath = await download.path();           
          // stream filePath directly to storage — no HTTP re-download needed                                                                                                                                                                                                                                                                                                                         
          return;                                                                                                                                                                                                                                                                                                                                                                                     
      }                                                                                                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                                                      
      // fallback: re-enqueue for HTTP download                                                                                                                                                                                                                                                                                                                                                       
      await reenqueueRequestAsDownload(request);
  }        

Proposed solution might be to expose download on the errorHandler context when the error is "Download is starting" or a dedicated downloadHandler hook, analogous to requestHandler

nikitachapovskii-dev avatar Apr 20 '26 13:04 nikitachapovskii-dev

@barjin @janbuchar would love to hear your thoughts about it

Happy to implement this myself if the direction makes sense to you.

nikitachapovskii-dev avatar Apr 20 '26 13:04 nikitachapovskii-dev

It shouldn't be too hard to expose a collection of Download objects on the playwright crawling context. One caveat I can see is that very large files will be saved to disk. The re-enqueueing strategy utilizes streams to circumvent that.

janbuchar avatar Apr 21 '26 08:04 janbuchar

Thanks for checking @janbuchar ! Just to clarify — createReadStream() still writes to disk, it just lets you start streaming to storage while the download is in progress rather than waiting for it to complete. If that's not a problem (I think it's not) I'll prepare a PR

nikitachapovskii-dev avatar Apr 21 '26 12:04 nikitachapovskii-dev

I'd be wary of having two independent code paths responsible for downloading (as in WCC, the canonical way of downloading files is the FileDownloader instance, which prints the download progress, sets the right content type, scales up / down, retries in case of failure...).

Imo the "Crawlee" way of solving the problem would be reenqueuing the request to the FileDownloader instance with all the right cookies and HTTP headers. This should be possible even now (the parameter to addRequest can, afaik, contain headers).

Have you considered this approach when debugging the original issue? Were there any problems?

barjin avatar Apr 21 '26 14:04 barjin

Hey @barjin, thanks for taking a look

Imo the "Crawlee" way of solving the problem would be reenqueuing the request to the FileDownloader instance with all the right cookies and HTTP headers. This should be possible even now (the parameter to addRequest can, afaik, contain headers).

yes tried it, but without headers, now tried with headers but still getting text/html from FileDownloader.

Looks like the session lives on ecas.ec.europa.eu, so my attempt to get page.context().cookies(url) doesn't return the right credentials

2026-04-22T08:18:25.886Z DEBUG PlaywrightCrawler:SessionPool:Session: Could not set cookies. {"errorMessages":["Cookie not in this host's domain. Cookie:ecas.ec.europa.eu Request:eur-lex.europa.eu"]}

nikitachapovskii-dev avatar Apr 22 '26 08:04 nikitachapovskii-dev