How to upload a file with puppeteer and dropzone?
Asked Answered
U

3

7

I use puppeteer and I have a dropzone form.

I want to add a file from chrome headless to the dropzone form.

How can I do that?

Note: The form contains some actions in some dropzone events (when added file).

Unhouse answered 16/9, 2017 at 18:36 Comment(1)
Could you show a minimal reproducible example of the dropzone form you want to upload to along with a code snippet of your attempt? Thanks!Amadis
F
1

Not sure if I understood the problem correctly, but try this:

const dropZoneInput = await page.$(inputID);
dropZoneInput.uploadFile(absolutePathToFile);
Findlay answered 20/11, 2017 at 10:19 Comment(1)
This is not working for me. I did not trigger the dropzone.js feature.Hearne
C
0

I have it working with puppeteer:

const fileInput = await page.$(
    ".{yourDropzoneClassName} input[type=file]"
);
await fileInput.uploadFile("path/to/file");
Cambridge answered 21/10, 2018 at 13:9 Comment(1)
Upvoting since there's no good reason to downvote this with no explanation.Maddie
K
0
(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    ignoreDefaultArgs: true
  });
  const page = await browser.newPage();
  await page.goto('https://react-dropzone.js.org/');
  await page.waitForSelector('input[type=file]');
  const fileInput  = await page.$('#rsg-root > div > main > section > section:nth-child(3) > section > section:nth-child(1) > article > div:nth-child(2) > div.rsg--preview-60 > div > section > div > input[type=file]');
  await fileInput.uploadFile("./test/playground.js");

  ///trigger event
  await fileInput.evaluate(upload => upload.dispatchEvent(new Event('change', { bubbles: true })));
  ///

  await page.close();
})();
Kwakiutl answered 12/4, 2021 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.