How to register a file upload implemented in dropzone from papeteer

Asked 2 years ago, Updated 2 years ago, 116 views

I have implemented the function of accessing the page from papeteer and registering by automatically entering the form, but I am worried about how to register the image for the file upload implemented in dropzone.

How to upload the file itself is

consteleHandle=awit page.$('fileInput');
eleHandle.uploadFile('uploadFile');

I know it can be implemented in this way, but the form implemented in dropzone does not have a file form.

The way to upload an image is to click on the dropzone area and select it from the file dialog, or drag and drop the image to the dropzone area, but I think it would be difficult for Puppeteer to approach them and upload the image.

If you have a good solution, I would appreciate it if you could let me know.

javascript puppeteer

2022-09-30 20:10

1 Answers

It may have already been resolved, but in my case, I was forced to add the input[type=file] element on the spot and run uploadFile and convert the change event to Dropzone.

Add // input [type=file]
wait page.validate(()=>{
  let fileInput= document.createElement('input')
  fileInput.id = 'dummy-file-input'
  fileInput.type='file'

  // Convert change event to drop event to dropzone
  fileInput.onchange=(e)=>{
    // Map application fails inside Dropzone unless FileList is converted to Array
    let files = [ ]
    for (vari=0;i<e.target.files.length;i++) {files.push(e.target.files[i])}

    let dropEvent= document.createEvent('UIEvents')
    dropEvent.initUIEvent('drop', false, false)
    dropEvent.dataTransfer={files:files}

    let dropZone= document.querySelector('.dropzone.dz-clickable')
    dropZone.dispatchEvent (dropEvent)
  }
  document.body.appendChild(fileInput)
})

// Set the file for the added input [type=file].
let fileInput = wait page.$('#dummy-file-input')
wait fileInput.uploadFile(filePath)


2022-09-30 20:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.