Upload file with pure javascript, (Crawler) inputar file

Asked

Viewed 299 times

0

I am crawling a web page and there comes a time when I need to upload a file, this Crawler is to test a system. What I need to do is with the pure javascript inputar a document on the following page:

<label for="avatar">Choose a profile picture:</label>

<input type="file"
       id="avatar" name="avatar"
       accept="image/png, image/jpeg">

Does anyone know how to do that ?

Thanks in advance.

  • You want a script that accesses this URL and sends a predetermined file to the server that hosts this page, that’s what you want?

  • There is no way to input files from the client’s machine, if this were possible it would be one of the biggest security loopholes that a browser would have.

  • Missing the main, the code you already have! Using what? Puppeteer? Phantomjs? Selenium? Anyway, you need the code you already have.

  • I use a class that was developed with dryscrap , I already have the code that navigates the page and fills normal fields , this class I use has a function that allows you to run pure javascript on the screen, this javascript that I use to fill the fields, however simulating a file is complicated, with Puppeteer I can go up because it already has a function of its own.

  • In Puppeteer it would be like this var findelement = await page.$("#file"); &#xA; await findelement.uploadFile("teste.pdf"); //Caminho do arquivo relativo ao script.

1 answer

0


Guys I managed to solve.

Basically what I had to do was to perform an ajax request on the file I wanted to insert forcing the request to return a bluble object (which is the file in binary format).

After that with this file in hand I checked the upload function that was called in the upload, called this function by passing the parameters and my binary file. With that he went to the server.

I appreciate the support.

  • It would be interesting to post the code in the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.