Submit a form with Puppeteer

Standard

Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium. Puppeteer runs headless by default, but can be configured to run a full browser.

A thing you will need to do when using Puppeteer is filling out and submit forms. It’s straightforward to fill fields but sometimes it’s difficult to submit the form. The submit input doesn’t have an id, it’s enclosed in multiple tags, there are multiple buttons (like a search bar in the header). An easy thing to do is to reproduce what you would do naturally: press the Enter button to submit the form.

Here is the code to do this:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.goto('https://www.google.com/admin');

  await page.type('#username', '[email protected]');
  await page.type('#password', 'password');
  await page.keyboard.press('Enter');

  await page.waitForNavigation();
  console.log('New Page URL:', page.url());
  await browser.close();
})();

Sounds great? Give me a follow on Twitter or learn more about me.