Skip to content

Instantly share code, notes, and snippets.

@jecfish
Last active October 15, 2022 21:37
Show Gist options
  • Save jecfish/419995d869060a9fb6bcd35e65176aba to your computer and use it in GitHub Desktop.
Save jecfish/419995d869060a9fb6bcd35e65176aba to your computer and use it in GitHub Desktop.
Puppeteer Text Selector
import puppeteer from 'puppeteer';
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://developer.chrome.com/blog/extend-recorder/');
// Use unique text as selector
// It's not neccessary to specify full text in the selector
const textSelector = await page.waitForSelector('text/Chrome DevTools Recorder');
// Get full text content
const fullTitle = await textSelector.evaluate(el => el.textContent);
console.log('Blog post title: %s', fullTitle);
// -> Blog post title: Customize and automate user flows beyond Chrome DevTools Recorder
// How about this one? Guess the result 😏
const tryThisSelector = await page.$('text/YeenDev');
console.log(await tryThisSelector.evaluate(el => el.innerHTML));
await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment