How to upload a file in Puppeteer?

To upload a file in Puppeteer, you can use the input element to select the file and then use the page.click method to trigger the file upload dialog.

Here's an example code to upload a file using Puppeteer:

const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); // Go to the page where you want to upload the file await page.goto('https://www.example.com/upload'); // Select the file input element const inputFile = await page.$('input[type="file"]'); // Upload the file await inputFile.uploadFile('path/to/your/file.jpg'); // Click the submit button to upload the file await page.click('input[type="submit"]'); await browser.close(); })();

In this code snippet, we first launch a Puppeteer browser and navigate to the page where we want to upload the file. We then select the file input element using page.$ and use the uploadFile method to upload the file to that element. Finally, we click on the submit button to trigger the file upload.

Make sure to replace 'https://www.example.com/upload' with the URL of the page where you want to upload the file and 'path/to/your/file.jpg' with the actual path to the file you want to upload.