Skip to content

Instantly share code, notes, and snippets.

@jsoverson
Created June 20, 2019 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jsoverson/68caef4c0b259bc33e0b4a9d96a7745b to your computer and use it in GitHub Desktop.
Save jsoverson/68caef4c0b259bc33e0b4a9d96a7745b to your computer and use it in GitHub Desktop.
Example puppeteer setup for Joe McCann
const puppeteer = require('puppeteer');
const config = {
entryUrl: '',
auth: {
email: '',
emailSelector: '',
password: '',
passwordSelector: '',
submitButtonSelector: ''
},
dataSelector: '',
inputSelector: '',
input: '',
inputSubmitSelector: '',
modalTriggerSelector: '',
targetSelector: '',
screenShotPath: ''
};
(async () => {
const browser = await puppeteer.launch({
headless: false, // remove this once this works
defaultViewport: null, // account for wonky viewport bug on mac
});
const [ page ] = await browser.pages();
// Start page
await page.goto(config.entryUrl);
// Auth
await page.type(config.emailSelector, config.email);
await page.type(config.passwordSelector, config.password);
await page.click(config.submitButtonSelector);
// This may be better as a waitForResponse();
// https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagewaitforresponseurlorpredicate-options
await page.waitForNavigation();
// Click on the Data element. You may be able to just navigate directly to the URL
// if the app allows it
await page.click(config.dataSelector);
// same as above wrt:waitForResponse();
await page.waitForNavigation();
// Enter the "DAYS" input and click on the button
await page.type(config.inputSelector, config.input);
await page.click(config.dataSelector);
// same wrt:waitForResponse();
await page.waitForNavigation();
// Click on selector for "Bitmex funding rate"
await page.click(config.modalTriggerSelector);
// This is probably better waiting for something specific about the modal/graphic
// maybe an animation end or an event.
await page.waitFor(1000);
// Grab the element that we want to screenshot
const target = page.$(config.targetSelector);
// You can omit the path and have this return a buffer
await target.screenshot({path: config.screenShotPath});
await browser.close();
})();
@jsoverson
Copy link
Author

The awaits can be combined in whatever promise-managing way you want. I left them all flat because it'll be easier to test and cut/copy/paste during troubleshooting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment