question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

uploadFile doesn't work with puppeteer.connect

See original GitHub issue

Steps to reproduce

Currently when used inputElement.uploadFile via puppeteer.connect, it uploads an empty file, as it doesn’t exist on host machine. I believe this is a DevTools limitation and there’s no possible workaround except uploading the file to remote puppeteer’s host.

What steps will reproduce the problem?

  1. const page = await puppeteer.connect(/* ... */);
  2. const input = await page.$('input[type="file"]');
  3. await input.uploadFile('./this/will/upload/an/empty/file');

What is the expected result?

  • uploaded files are serialised before sent to devtools instead of their file paths,
  • or an error thrown saying this is not supported via connect.

What happens instead?

Puppeteer silently uploads an empty payload.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
bloeyscommented, Apr 23, 2020

Thanks a lot for the tip @mathiasbynens, it was really helpful!

For anyone facing issues uploading with puppeteer, here is the code I went with that finally worked

const fileToUpload = {
  name: 'myFileName',
  content: fs.readFileSync('/file/path').toString(),
  mimeType: 'text/plain', // This will depend on your case
}

await page.evaluate(async (fileToUpload) => {

  //Create blob and file from file string
  const b = new Blob([fileToUpload.content], { type: fileToUpload.mimeType })
  const dt = new DataTransfer();
  dt.items.add(new File([b], fileToUpload.name));

  //Simulate drag and drop on your input field using DataTransfer class
  ele = document.querySelector('input[type=file]')
  ele.files = dt.files;
  ele.dispatchEvent(new Event('input', { bubbles: true }));
  ele.dispatchEvent(new Event('change', { bubbles: true }));
}, fileToUpload);

I hope this helps someone, instead of spending days working with and around puppeteer file upload.

However, @mathiasbynens do you have an idea of why the builtin calls (I see there is a CDP call in newer versions) in puppeteer don’t work? It would be nice to understand why that happened.

Thanks again for your help.

2reactions
joelgriffithcommented, Jul 25, 2020

Since puppeteer verifies that files are present on its local file-system, then this breaks connected sessions in an unintuitive way. Versions <= 3.0.0 handled this pretty well, but at the cost of sending more data over the network.

Is it possible to make it more graceful? For instance, if it’s aconnected session then do the DataTransfer method, otherwise puppeteer can just pass the list of files over to chromium? Happy to do a PR, just don’t want to waste the time if it’s not viable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Puppeteer file-chooser upload not working - Stack Overflow
I try to upload a video on instagram using puppeteer with FileChooser: const selectBtn = await page.$x("selector.
Read more >
Practical Puppeteer: How to upload a file programatically
Today I will share about how to upload file using Puppeteer. If you don't know about Puppeteer yet, here is the brief explanation....
Read more >
How to perform FileUpload testing with Puppeteer - YouTube
In this video, we will discuss working with FileUpload in Puppeteer.In this video we will discuss Working with XPath in Puppeteer automation ...
Read more >
Node.js Puppeteer FileChooser Upload Files & Images to ...
Watch next – [Popular Videos on the Channel] [ Login with Google Account using Javascript] ... [ Uploading Files using Google Drive Api] ......
Read more >
Puppeteer documentation - DevDocs
Puppeteer 7.1.0 API documentation with instant search, offline support, keyboard shortcuts, mobile version, and more.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found