Skip to content

Instantly share code, notes, and snippets.

@sorenlouv
Last active September 6, 2018 13:17
Show Gist options
  • Save sorenlouv/94ab2b9c5e88c1c119182425b19bcd59 to your computer and use it in GitHub Desktop.
Save sorenlouv/94ab2b9c5e88c1c119182425b19bcd59 to your computer and use it in GitHub Desktop.
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