How to upload a random file from Directory then delete it Puppeteer?

Viewed 27

So I have a directory with images: C:\Users\username\Desktop\mpimages

I need 1 random image to be uploaded then deleted from the folder.

This is the class which is clicked which brings up the file browser window to upload file < div class="om3e55n1" >

I've found this solution online but it's not working: fs.readdirSync is not a function.

const rnd = (arr) => arr[Math.floor(Math.random() * arr.length)];
const rndFile = () => `${dir}/${rnd(fs.readdirSync(dir))}`;

(async () => {
    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();
    await page.goto("YOUR/UPLOAD/URL")
    
  const file = rndFile();
  const elementHandle = await page.$("input[type=file]");
  await elementHandle.uploadFile(file);
  await page.click("input[type=submit]")
  
  fs.unlinkSync(file)
})();
0 Answers
Related