docta_faustus docta_faustus - 6 months ago 356
Node.js Question

Puppeteer: How to submit a form?

Using puppeteer, how could you programmatically submit a form? So far I've been able to do this using'.input[type="submit"]')
if the form actually includes a submit input. But for forms that don't include a submit input, focusing on the form text input element and using'Enter')
doesn't seem to actually cause the form to submit:

const puppeteer = require('puppeteer');

(async() => {

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('', {waitUntil: 'load'});

// Type our query into the search bar
await page.focus('.js-search-field');
await page.type('puppeteer');

// Submit form

// Wait for search results page to load
await page.waitForNavigation({waitUntil: 'load'});

console.log('FOUND!', page.url());

// Extract the results from the page
const links = await page.evaluate(() => {
const anchors = Array.from(document.querySelectorAll('.result-link a'));
return => anchor.textContent);


Answer Source

Try this

const form = await page.$('form-selector');
await form.evaluate(form => form.submit());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download