Created
July 12, 2023 15:10
-
-
Save kmsheng/0a642c2c302cee2f538cecd33a3e899b to your computer and use it in GitHub Desktop.
Puppteer example
This file contains hidden or 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
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