Skip to content

Instantly share code, notes, and snippets.

@osieckiAdam
Created December 8, 2019 12:55
Show Gist options
  • Select an option

  • Save osieckiAdam/9e033f890311b9d65a6d98eba4463d09 to your computer and use it in GitHub Desktop.

Select an option

Save osieckiAdam/9e033f890311b9d65a6d98eba4463d09 to your computer and use it in GitHub Desktop.
Puppeteer example - display all badges from traiblazer.me website
const puppeteer = require("puppeteer");
const run = async () => {
//set up puppeteer and page
//remove the parameter from the function to run in the headless mode
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.setViewport({
width: 1920,
height: 1080,
deviceScaleFactor: 1
});
//notify when page is loaded
page.once("load", () => console.log("PAGE LOADED"));
await Promise.all([
page.goto("https://trailblazer.me/id/aosiecki"),
page.waitForNavigation({ waitUntil: "networkidle0" })
]);
await shouldClickTheButton(page);
};
async function shouldClickTheButton(page) {
//not using standard selectors here, because they are not working
//if DOM element is stored inside web component
//we need to get all the components containing the element that we are looking for
const selector = `document.querySelector('c-lwc-trailhead-badges').shadowRoot.querySelector('c-lwc-card').querySelector('c-lwc-card-footer-link')`;
const buttonHandle = await page.evaluateHandle(selector);
//getting the inner text from the found element
let buttonStatus = await buttonHandle.getProperty("innerText");
if (buttonStatus == "JSHandle:Show More") {
await buttonHandle.click();
setTimeout(function() {
shouldClickTheButton(page);
}, 100);
} else if (buttonStatus == "JSHandle:Loading") {
setTimeout(function() {
shouldClickTheButton(page);
}, 100);
} else if (buttonStatus == "JSHandle:Show Less") {
//PERFORM YOUR LOGIC HERE, ALL OF THE BADGES ARE DISPLAYED NOW
//uncomment next line to close the page after performing Your logic
//await page.close();
}
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment