Upload an image with Puppeteer

03 Aug 2021 in TIL

Automate file uploads using Puppeteer and Node.js

Here's how to upload an image using Puppeteer:

javascript
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto("https://example.com/your-page");
await page.waitForSelector("input[type=file]");
const input = await page.$("input[type=file]");
await input.uploadFile("/path/to/file");

This is extracted from the github-social-image cli