2024-02-05 09:26:37 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2024 Google Inc.
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-04-14 16:42:48 +00:00
|
|
|
const puppeteer = require('puppeteer');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To have Puppeteer fetch a Firefox binary for you, first run:
|
|
|
|
*
|
2024-01-08 09:02:56 +00:00
|
|
|
* PUPPETEER_PRODUCT=firefox npm install
|
2020-04-14 16:42:48 +00:00
|
|
|
*
|
|
|
|
* To get additional logging about which browser binary is executed,
|
|
|
|
* run this example as:
|
|
|
|
*
|
2024-01-08 09:02:56 +00:00
|
|
|
* DEBUG=puppeteer:launcher NODE_PATH=../ node examples/cross-browser.js
|
2020-04-14 16:42:48 +00:00
|
|
|
*
|
|
|
|
* You can set a custom binary with the `executablePath` launcher option.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const firefoxOptions = {
|
|
|
|
product: 'firefox',
|
|
|
|
extraPrefsFirefox: {
|
|
|
|
// Enable additional Firefox logging from its protocol implementation
|
|
|
|
// 'remote.log.level': 'Trace',
|
|
|
|
},
|
|
|
|
// Make browser logs visible
|
|
|
|
dumpio: true,
|
|
|
|
};
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
(async () => {
|
2020-04-14 16:42:48 +00:00
|
|
|
const browser = await puppeteer.launch(firefoxOptions);
|
|
|
|
|
|
|
|
const page = await browser.newPage();
|
|
|
|
console.log(await browser.version());
|
|
|
|
|
|
|
|
await page.goto('https://news.ycombinator.com/');
|
|
|
|
|
|
|
|
// Extract articles from the page.
|
2022-11-17 12:26:08 +00:00
|
|
|
const resultsSelector = '.titleline > a';
|
2022-06-22 13:25:44 +00:00
|
|
|
const links = await page.evaluate(resultsSelector => {
|
2020-04-14 16:42:48 +00:00
|
|
|
const anchors = Array.from(document.querySelectorAll(resultsSelector));
|
2022-06-22 13:25:44 +00:00
|
|
|
return anchors.map(anchor => {
|
2020-04-14 16:42:48 +00:00
|
|
|
const title = anchor.textContent.trim();
|
|
|
|
return `${title} - ${anchor.href}`;
|
|
|
|
});
|
|
|
|
}, resultsSelector);
|
|
|
|
console.log(links.join('\n'));
|
|
|
|
|
|
|
|
await browser.close();
|
|
|
|
})();
|