Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload file with JS/Puppeteer

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?

like image 457
user3562806 Avatar asked Dec 10 '19 18:12

user3562806


People also ask

Can we upload file using AJAX?

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.

How do I download files from puppeteer?

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.


1 Answers

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.

like image 57
Thomas Dondorf Avatar answered Sep 20 '22 04:09

Thomas Dondorf