puppeteer upload file input code example

Example 1: puppeter example fileupload

const puppeteer = require('puppeteer')
const path = require('path')
const fs = require('fs')
const pathFile = __dirname.split('\\').slice(0, 4).join('/')
const fileExitst = fs.existsSync(`${pathFile}/travis_1.png`)

const scraperRunner = async () => {
	const browser = await puppeteer.launch({ headless: false, args: ['--no-sanbox'] })
	const page = await browser.newPage()
	await page.goto('https://anonfiles.com', { waitUntil: 'networkidle2' })

	await page.waitForSelector('input[type="file"]')
	const input = await page.$('input[type="file"]')
	if (fileExitst) {
		await input.uploadFile(`${pathFile}/travis_1.png`)
	}
}

scraperRunner()

Example 2: puppeteer example multiple file upload

await page.waitForSelector('input[type="file"]')
  const files = await Promise.all([`${filePath}/travis_1.png`, `${filePath}/travis_2.png`])
  const input = await page.$('input[type="file"]')
  if (fileExists) {
    await input.uploadFile(...files)
  }