Skip to content

Instantly share code, notes, and snippets.

@kmsheng
Created July 12, 2023 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmsheng/0a642c2c302cee2f538cecd33a3e899b to your computer and use it in GitHub Desktop.
Save kmsheng/0a642c2c302cee2f538cecd33a3e899b to your computer and use it in GitHub Desktop.
Puppteer example
const puppeteer = require('puppeteer-extra')
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
puppeteer.use(StealthPlugin)
;(async () => {
const browser = await puppeteer.launch({
headless: false,
executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
})
const page = await browser.newPage()
await page.setViewport({ width: 1080, height: 1024 })
await page.goto('', { waitUntil: 'networkidle2' })
await page.waitForSelector('[aria-label="subject answer container"]')
await autoScroll(page);
await page.click('[aria-label="subject answer container"] > div > div:nth-child(3)')
await page.click('button[data-qa="submit"]')
})();
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function autoScroll(page){
await page.evaluate(async () => {
await new Promise(resolve => {
let totalHeight = 0;
var distance = 100;
var timer = setInterval(() => {
var scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if (totalHeight >= scrollHeight - window.innerHeight){
clearInterval(timer);
resolve();
}
}, 100)
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment