Created
January 15, 2024 00:58
-
-
Save zeckdude/d83e5a32b68af1a6422e14b4c128e715 to your computer and use it in GitHub Desktop.
Puppeteer Demo update for new page structure
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
// This is an update to the script on the Puppeteer at https://pptr.dev/#example which is relying on an old page structure | |
import puppeteer from 'puppeteer'; | |
export const runScrape = async (res) => { | |
// Launch the browser and open a new blank page | |
const browser = await puppeteer.launch({headless: true}); | |
const page = await browser.newPage(); | |
// Navigate the page to a URL | |
await page.goto('https://developer.chrome.com/'); | |
// Set screen size | |
await page.setViewport({width: 1080, height: 1024}); | |
// Type into search box | |
await page.type('.devsite-search-field', 'automate beyond recorder'); | |
await page.keyboard.press('Enter'); | |
// Wait and click on first result | |
const searchResultSelector = 'a.gs-title'; | |
await page.waitForSelector(searchResultSelector); | |
await page.click(searchResultSelector); | |
// Locate the title text | |
const textSelector = await page.waitForSelector( | |
'text/Customize and automate' | |
); | |
const fullTitle = await textSelector?.evaluate(el => el.textContent); | |
// Print the full title | |
const message = `The title of this blog post is "${fullTitle}".` | |
console.log(message); | |
res.send(message); | |
await browser.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment