Created
January 20, 2025 00:40
-
-
Save notpike/97a61388ea85cfc258ae3bfb488dc45d to your computer and use it in GitHub Desktop.
NodeJS Puppeteer 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 { faker } = require('@faker-js/faker'); | |
| const puppeteer = require('puppeteer'); | |
| const puppeteerExtra = require('puppeteer-extra'); | |
| const Stealth = require('puppeteer-extra-plugin-stealth'); | |
| puppeteerExtra.use(Stealth()); | |
| //Vars | |
| const grades = ['Kindergarten', '1st', '2nd', '3rd', '4th', '5th', '6th', '7th','8th', '9th', '10th', '11th', '12th']; | |
| const schoolType = ['Public', 'Private', 'Charter', 'Other']; | |
| const sports = ['Soccer', 'Football', 'Baseball', 'Softball', 'Volleyball', 'Swim', 'Waterpolo', 'Track and Field', 'Cross Country', 'Basketball', 'Cheer', 'Gymnastics', 'Boxing', 'Hockey', 'Other']; | |
| const parent = ['Mother', 'Father', 'Guardian', 'Other']; | |
| const laAreaCodes = ['805', '820', '661', '442', '760', '747', '818', '626', '840', '909', '310', '424', '213', '323', '562', '657', '714', '949', '951', '442', '760']; | |
| // Main Loop | |
| (async () => { | |
| // Narative | |
| var areaCode = laAreaCodes[Math.floor(Math.random()*laAreaCodes.length)]; // Both Parent and Child will have same area code | |
| var parentLastName = faker.person.lastName(); // Parent's last name | |
| var parentRelationship = parent[Math.floor(Math.random()*parent.length)]; // Parent Relationship | |
| if(['Guardian', 'Other'].includes(parentRelationship)) { // Kid's last name if not blood related | |
| var studentLastName = faker.person.lastName(); | |
| var parentFirstName = faker.person.firstName(); // Randome first name for guardian or other | |
| } else { | |
| var studentLastName = parentLastName; // Kid's last name if blood related | |
| } | |
| if(parentRelationship === 'Mother') { // Mom's name | |
| var parentFirstName = faker.person.firstName('female'); | |
| } else if(parentRelationship === 'Father') { | |
| var parentFirstName = faker.person.firstName('male'); // Dad's name | |
| } | |
| var studentFirstName = faker.person.firstName('female'); // Kid's first names are always female | |
| var grade = grades[Math.floor(Math.random()*grades.length)]; // Grade | |
| var yearDOB = 2025-(5+grades.indexOf(grade)); // Year born | |
| var studentDOB = Math.floor(Math.random() * 12).toString() + // DOB | |
| Math.floor(Math.random() * 27).toString() + | |
| yearDOB.toString(); | |
| var sport = sports[Math.floor(Math.random()*sports.length)]; // Sport being played | |
| // Puppeteer | |
| const browser = await puppeteer.launch({ headless: false }); | |
| const page = await browser.newPage(); | |
| await page.setViewport({ width: 1920, height: 1080 }); // Set Browser Size | |
| await page.setUserAgent( | |
| 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' | |
| ); | |
| //await page.setUserAgent(faker.internet.userAgent()); // Random userAgent (reCAPTCHA cought this) | |
| await page.goto('https://website.com'); // Open Form | |
| await page.waitForNetworkIdle(); // Wait to fully load | |
| // Form Input | |
| // Student Info | |
| await page.type('input[id="name-ee367545"]', studentFirstName); // Student First Name | |
| await page.type('input[id="name-ee367545"]', studentLastName); // Student Last Name | |
| await page.type('input[id="date-6704a7a7"]', studentDOB); // Student DOB | |
| let i = await page.$('select[id="select-d028169d"]'); // Grade | |
| await i.type(grade); | |
| await page.type('input[id="phone-64f8a1c8"]', areaCode + Math.floor(Math.random() * 9999999).toString() ); // Student Phone # | |
| await page.type('input[id="email-f5dd562d"]', faker.internet.email()); // Student Email | |
| // School Info | |
| await page.type('input[id="text-4e1582df"]', 'King'); // School District | |
| await page.type('input[id="text-ab1bef83"]', 'Bear'); // School Name | |
| i = await page.$('select[id="select-4b04c9f9"]'); // School Type | |
| await i.type(schoolType[Math.floor(Math.random()*schoolType.length)]) | |
| i = await page.$('select[id="select-54a79dfb"]'); // Are you a student in CA? | |
| await i.type('Yes'); | |
| // Sport Info | |
| i = await page.$('select[id="select-5233d989"]'); // Sport | |
| await i.type(sport); | |
| i = await page.$('select[id="select-bcd3d894"]'); // Trans student in sport? | |
| await i.type('Yes'); | |
| i = await page.$('select[id="select-7aa80927"]'); // Trans student on your team? | |
| await i.type('Yes'); | |
| i = await page.$('select[id="select-d0916576"]'); // Has a trans student beat you? | |
| await i.type('Yes'); | |
| await page.type('textarea[id="textarea-61bfe773"]', 'Text Area Input'); // Explain | |
| // Parent Info | |
| i = await page.$('select[id="select-2e6db164"]'); // Parent Info | |
| await i.type(parentRelationship) | |
| await page.type('input[id="name-yui"]', parentFirstName); // Parent First Name | |
| await page.type('input[id="name-yui"]', parentLastName); // Parent Last Name | |
| await page.type('input[id="email-dd630fb5"]', faker.internet.email()); // Parent Email | |
| await page.type('input[id="phone-b7c1fc30"]', areaCode + Math.floor(Math.random() * 9999999).toString()); // Parent Phone # | |
| // Signatures | |
| await page.type('input[id="ee937708"]', studentFirstName + ' ' + studentLastName); // Student Sig | |
| await page.type('input[id="text-726b8518"]', parentFirstName + ' ' + parentLastName); // Parent Sig | |
| // Submit | |
| // Wait for 15 seconds | |
| // const delay = ms => new Promise(resolve => setTimeout(resolve, ms)) | |
| // await delay(15000) | |
| // await page.hover('button.submit'); | |
| // await page.click('button.submit'); | |
| // TODO, reCAPTCHA bypass for Squarespace (May need mouse movment before click();) | |
| await page.evaluate(() => { | |
| document.querySelector('button[type=submit]').click(); | |
| }); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment