cross-browser.js (1375B)
1 /** 2 * @license 3 * Copyright 2024 Google Inc. 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 const puppeteer = require('puppeteer'); 7 8 /** 9 * To have Puppeteer fetch a Firefox binary for you, first run: 10 * 11 * npx puppeteer browsers install firefox 12 * 13 * To get additional logging about which browser binary is executed, 14 * run this example as: 15 * 16 * DEBUG=puppeteer:launcher NODE_PATH=../ node examples/cross-browser.js 17 * 18 * You can set a custom binary with the `executablePath` launcher option. 19 */ 20 21 const firefoxOptions = { 22 browser: 'firefox', 23 extraPrefsFirefox: { 24 // Enable additional Firefox logging from its protocol implementation 25 // 'remote.log.level': 'Trace', 26 }, 27 // Make browser logs visible 28 dumpio: true, 29 }; 30 31 (async () => { 32 const browser = await puppeteer.launch(firefoxOptions); 33 34 const page = await browser.newPage(); 35 console.log(await browser.version()); 36 37 await page.goto('https://news.ycombinator.com/'); 38 39 // Extract articles from the page. 40 const resultsSelector = '.titleline > a'; 41 const links = await page.evaluate(resultsSelector => { 42 const anchors = Array.from(document.querySelectorAll(resultsSelector)); 43 return anchors.map(anchor => { 44 const title = anchor.textContent.trim(); 45 return `${title} - ${anchor.href}`; 46 }); 47 }, resultsSelector); 48 console.log(links.join('\n')); 49 50 await browser.close(); 51 })();