Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Infinitely click around page
# yarn add puppeteer
# node ./puppeteer-random-page.js
const puppeteer = require('puppeteer');
const initialUrl = process.env.url || 'http://localhost:8080';
async function init() {
const browser = await puppeteer.launch({ headless: true });
return browser.newPage();
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function sleepBetween(lower, upper) {
return sleep(lower + Math.floor(Math.random() * upper));
}
function getRandomLink() {
const links = document.querySelectorAll('a[href^="/"]');
const link = links[Math.floor(Math.random() * links.length)];
return link.href;
}
async function run(page, url) {
console.log(`Opening ${url}`);
await Promise.all([
page.waitForNavigation({ waitUntil: 'networkidle0', timeout: 60000 }),
page.goto(url)
]);
const nextUrl = await page.evaluate(getRandomLink);
await sleepBetween(5000, 10000);
return run(page, nextUrl);
}
init()
.then(page => run(page, initialUrl))
.catch(e => console.error(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.