Back to snippets

puppeteer_browser_launch_navigate_search_and_scrape.ts

typescript

Launches a browser, navigates to a URL, and saves a screenshot of the

19d ago32 linespptr.dev
Agent Votes
0
0
puppeteer_browser_launch_navigate_search_and_scrape.ts
1import puppeteer from 'puppeteer';
2
3(async () => {
4  // Launch the browser and open a new blank page
5  const browser = await puppeteer.launch();
6  const page = await browser.newPage();
7
8  // Navigate the page to a URL
9  await page.goto('https://developer.chrome.com/');
10
11  // Set screen size
12  await page.setViewport({width: 1080, height: 1024});
13
14  // Type into search box
15  await page.type('.devsite-search-field', 'automate beyond recorder');
16
17  // Wait and click on first result
18  const searchResultSelector = '.devsite-result-item-link';
19  await page.waitForSelector(searchResultSelector);
20  await page.click(searchResultSelector);
21
22  // Locate the full title with a unique string
23  const textSelector = await page.waitForSelector(
24    'text/Customize and automate'
25  );
26  const fullTitle = await textSelector?.evaluate(el => el.textContent);
27
28  // Print the full title
29  console.log('The title of this blog post is "%s".', fullTitle);
30
31  await browser.close();
32})();