Skip to content

Instantly share code, notes, and snippets.

@yickson
Created April 25, 2021 19:43
Show Gist options
  • Save yickson/938ae2d20029400010e4e540ba22a9c5 to your computer and use it in GitHub Desktop.
Save yickson/938ae2d20029400010e4e540ba22a9c5 to your computer and use it in GitHub Desktop.
Puppeteer in Cloud Function

Template for Google Cloud Function

Simple template of puppeteer in Cloud Function

I hope it helps you.

Packages:

  • Puppeteer 8.0.0
  • Node version 12
const puppeteer = require('puppeteer');
const urlLogin = 'https://myurl.com/';
exports.auth = async (req, res) => {
const {email, pass} = req.body;
console.log('Init puppeteer...')
const browser = await puppeteer.launch({ headless: false, slowMo: 10 }); //Change headless true to deploy in Cloud Function
try {
const page = await browser.newPage();
console.log('Browser to login');
await page.goto(urlLogin);
console.log('Insert data auth');
await page.type('input[name=identificacion]', email);
await page.type('input[name=password]', pass);
await page.click('.btn-iniciar-sesion');
console.log('Waiting for navigation...');
await page.waitForSelector('.avatar-miniatura');
console.log('Load service sucessfully!!!');
res.send({ response: 'success', data: null});
} catch (e) {
console.log(e);
res.send({response: 'error', data: 'puppeteer'});
} finally {
await browser.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment