有没有一种方法可以使用Puppeteer在React Dropzone中上传文件?

3

我尝试了一些在线建议,但都没有起作用。
目前我正在尝试这个:

  await page.goto('http://localhost:3000/');
  await page.waitForSelector('input[type=file]');
  const fileInput  = await page.$('input[type=file]');
  await fileInput.uploadFile("file.png");  

这段代码没有作用(或者说没有任何效果)。
有没有实现的方法?

1个回答

6

似乎需要给dropzone一点推动。您可以触发change事件,这样dropzone就知道文件输入已经更新了。

(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");

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

  await page.close();
})();

1
这个可行!我最初的想法是触发文件输入的更改,但我想我做错了。谢谢! - Nir Tzezana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接