I'm trying to figure out how to upload a picture file into an input dialog. It isn't possible to just type in the name and hit enter as I don't see a way in order to automate that with Puppeteer. I figure I will have to set some value as the picture but I'm not sure how to do that. Any ideas?
A JavaScript method must be coded to initiate the asynchronous Ajax based file upload; A component must exist on the server to handle the file upload and save the resource locally; The server must send a response to the browser indicating the JavaScript file upload was successful; and.
We can avoid the default download path by explicitly specifying the path in our script. Let's update our script to set the path. const puppeteer = require('puppeteer'); const path = require('path'); const downloadPath = path. resolve('./download'); async function simplefileDownload() { const browser = await puppeteer.
You upload a file by using elementHandle.uploadFile
.
Code Sample
const elementHandle = await page.$("input[type=file]");
await elementHandle.uploadFile('path/to/file');
await page.click('selector-of-submit-button'); // might not be necessary
Depending on whether the page directly react to the change of the element, you might need to click a button to submit the form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With