Created
June 20, 2019 16:27
-
-
Save jsoverson/68caef4c0b259bc33e0b4a9d96a7745b to your computer and use it in GitHub Desktop.
Example puppeteer setup for Joe McCann
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'); | |
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(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
await
s 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.