Skip to content

Instantly share code, notes, and snippets.

@aboglioli
Created May 13, 2021 18:37
Show Gist options
  • Save aboglioli/43c88d8443f25445233a25205997ac11 to your computer and use it in GitHub Desktop.
Save aboglioli/43c88d8443f25445233a25205997ac11 to your computer and use it in GitHub Desktop.
const puppeteer = require('puppeteer');
const proxy = ''; // '161.35.58.75:8080';
const url = '';
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
const scrape = async (browser, prefix) => {
const page = await browser.newPage();
console.log(`Visiting ${url}`);
await page.goto(url, {
timeout: 0,
waitUntil: ['load', 'domcontentloaded', 'networkidle0', 'networkidle2'],
});
console.log(`Filling form`);
await Promise.all([
page.waitForSelector("#document"),
sleep(2000),
]);
await page.screenshot({ path: `${prefix}_01.png` });
await page.type('#input1', '11223344');
await page.type('#input2', '11223344');
await page.evaluate(() => {
document.querySelector('#checkbox').click();
});
await sleep(1000);
await page.click('#submit');
console.log(`Form submitted`);
await page.screenshot({ path: `${prefix}_02.png` });
await sleep(5000);
// await page.waitForNavigation({
// timeout: 0,
// });
await page.screenshot({ path: `${prefix}_03.png` });
await page.close();
};
async function main() {
const browser = await puppeteer.launch({
args: [proxy ? `--proxy-server=${proxy}` : ''],
});
// Proxy
const proxyPage = await browser.newPage();
console.log('Checking IP address');
await proxyPage.goto('https://ifconfig.me', {
timeout: 0,
waitUntil: 'networkidle2',
});
await proxyPage.screenshot({ path: 'ip.png' });
await proxyPage.close();
// Page
for (let i = 0; i < 5; i++) {
console.log(`Scraping ${i}`);
await scrape(browser, i + 1);
}
await browser.close();
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment