Last active
September 6, 2018 13:17
-
-
Save sorenlouv/94ab2b9c5e88c1c119182425b19bcd59 to your computer and use it in GitHub Desktop.
Infinitely click around page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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