Skip to content

Instantly share code, notes, and snippets.

@zeckdude
Created January 15, 2024 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeckdude/d83e5a32b68af1a6422e14b4c128e715 to your computer and use it in GitHub Desktop.
Save zeckdude/d83e5a32b68af1a6422e14b4c128e715 to your computer and use it in GitHub Desktop.
Puppeteer Demo update for new page structure
// 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