How to upload a file with puppeteer and dropzone?

Viewed 4736

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

3 Answers

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

const dropZoneInput = await page.$(inputID);
dropZoneInput.uploadFile(absolutePathToFile);

I have it working with puppeteer:

const fileInput = await page.$(
    ".{yourDropzoneClassName} input[type=file]"
);
await fileInput.uploadFile("path/to/file");
(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();
})();
Related