|
| 1 | +--- |
| 2 | +title: Submitting a form with a file attachment |
| 3 | +description: Understand how to download a file, attach it to a form using a headless browser in Playwright or Puppeteer, then submit the form. |
| 4 | +menuWeight: 3 |
| 5 | +paths: |
| 6 | + - puppeteer-playwright/common-use-cases/submitting-a-form-with-a-file-attachment |
| 7 | +--- |
| 8 | + |
| 9 | +# Submitting a form with a file attachment |
| 10 | + |
| 11 | +We can use Puppeteer or Playwright to simulate submitting the same way a human-operated browser would. |
| 12 | + |
| 13 | +## [](#downloading-the-file) Downloading the file |
| 14 | + |
| 15 | +The first thing necessary is to download the file, which can be done using the `request-promise` module. We will also be using the `fs/promises` module to save it to the disk, so make sure they are included. |
| 16 | + |
| 17 | +```JavaScript |
| 18 | +import * as fs from 'fs/promises'; |
| 19 | +import request from 'request-promise'; |
| 20 | +``` |
| 21 | + |
| 22 | +The actual downloading is slightly different for text and binary files. For a text file, it can simply be done like this: |
| 23 | + |
| 24 | +```JavaScript |
| 25 | +const fileData = await request('https://some-site.com/file.txt'); |
| 26 | +``` |
| 27 | + |
| 28 | +For a binary data file, we need to provide an additional parameter so as not to interpret it as text: |
| 29 | + |
| 30 | +```JavaScript |
| 31 | +const fileData = await request({ |
| 32 | + uri: 'https://some-site.com/file.pdf', |
| 33 | + encoding: null |
| 34 | +}); |
| 35 | +``` |
| 36 | + |
| 37 | +In this case, `fileData` will be a `Buffer` instead of a string. |
| 38 | + |
| 39 | +To use the file in Puppeteer/Playwright, we need to save it to the disk. This can be done using the `fs/promises` module. |
| 40 | + |
| 41 | +```JavaScript |
| 42 | +await fs.writeFile('./file.pdf', fileData); |
| 43 | +``` |
| 44 | + |
| 45 | +## [](#submitting-the-form) Submitting the form |
| 46 | + |
| 47 | +The first step necessary is to open the form page in Puppeteer. This can be done as follows: |
| 48 | + |
| 49 | +```JavaScript |
| 50 | +const browser = await puppeteer.launch(); |
| 51 | +const page = await browser.newPage(); |
| 52 | +await page.goto('https://some-site.com/file-upload.php'); |
| 53 | +``` |
| 54 | + |
| 55 | +To fill in any necessary form inputs, we can use the `page.type()` function. This works even in cases when `elem.value = 'value'` is not usable. |
| 56 | + |
| 57 | +```JavaScript |
| 58 | +await page.type('input[name=firstName]', 'John'); |
| 59 | +await page.type('input[name=surname]', 'Doe'); |
| 60 | +await page. type( 'input[name=email]', '[email protected]'); |
| 61 | +``` |
| 62 | + |
| 63 | +To add the file to the appropriate input, we first need to find it and then use the [`uploadFile()`](https://pptr.dev/next/api/puppeteer.elementhandle.uploadfile) function. |
| 64 | + |
| 65 | +```JavaScript |
| 66 | +const fileInput = await page.$('input[type=file]'); |
| 67 | +await fileInput.uploadFile('./file.pdf'); |
| 68 | +``` |
| 69 | + |
| 70 | +Now we can finally submit the form. |
| 71 | + |
| 72 | +```JavaScript |
| 73 | +await page.click('input[type=submit]'); |
| 74 | +``` |
0 commit comments