Skip to content

Instantly share code, notes, and snippets.

@VesterDe
Last active February 23, 2020 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VesterDe/4daad7d758e74268f81293f1ab7a4201 to your computer and use it in GitHub Desktop.
Save VesterDe/4daad7d758e74268f81293f1ab7a4201 to your computer and use it in GitHub Desktop.
const selectorShot = async (page, s, path) => {
const frame = await page.mainFrame();
const el = await frame.$(s);
if (el) {
const box = await el.boundingBox();
console.log(JSON.stringify(box));
if (box) {
try {
await el.screenshot({
path: path,
});
}
catch (error){
console.log(error)
}
} else {
console.log(`Element not visible for capturing: ${s}`);
}
} else {
console.log(`Element not found for capturing: ${s}`);
}
};
const browser = await puppeteer.launch({ args: ["--no-sandbox", "--disable-setuid-sandbox"] });
const page = await browser.newPage();
await page.goto('https://www.nytimes.com');
await selectorShot(page, '.branding', "TEST1.png");
await selectorShot(page, '#site-index-navigation', "TEST2.png");
await browser.close();
@nsourov
Copy link

nsourov commented Feb 23, 2020

const puppeteer = require("puppeteer");

(async () => {
  const browser = await puppeteer.launch({ headless: false, devtools: true });
  const page = await browser.newPage();
  // Adjustments particular to this page to ensure we hit desktop breakpoint.
  await page.goto("http://www.producepriceindex.com/", {
    waitUntil: "networkidle2"
  });
  const handle = await page.$("[id=\"block-block-1\"]");
  await handle.screenshot({
    path: "element.png"
  });
  await browser.close();
})();

Try this.

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