Created
November 17, 2023 17:58
-
-
Save zemccartney/e0f0f66197012ee60c12675b1b11174a to your computer and use it in GitHub Desktop.
puppeteer-debug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const Puppeteer = require('puppeteer'); | |
module.exports = { | |
method: 'GET', | |
path: '/debug', | |
handler: async (request) => { | |
let browser; | |
try { | |
// Launch the browser and open a new blank page | |
browser = await Puppeteer.launch({ | |
headless: false, | |
slowMo: true, | |
args: ['--no-sandbox', '--disable-setuid-sandbox'] | |
}); | |
const page = await browser.newPage(); | |
// Navigate the page to a URL | |
await page.goto('https://developer.chrome.com/'); | |
if (!!request.query.error) { | |
throw new Error('boOM'); | |
} | |
// Set screen size | |
await page.setViewport({ width: 1080, height: 1024 }); | |
// Type into search box | |
await page.type('.search-box__input', 'autom'); | |
// Wait and click on first result | |
const searchResultSelector = '.search-box__link'; | |
await page.waitForSelector(searchResultSelector); | |
await page.click(searchResultSelector); | |
await browser.close(); | |
return `The title of this blog post is`; | |
} | |
finally { | |
if (browser) { | |
await browser.close(); | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment