Skip to content

Instantly share code, notes, and snippets.

@emmiep
Created March 3, 2018 15:29
Show Gist options
  • Save emmiep/cd35de612412ac6a283613a438e6acfa to your computer and use it in GitHub Desktop.
Save emmiep/cd35de612412ac6a283613a438e6acfa to your computer and use it in GitHub Desktop.
Use puppeteer to get coordinates of an element
const Puppeteer = require('puppeteer');
(async () => {
const browser = await Puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
const header = await page.$('h1');
const rect = await page.evaluate((header) => {
const {top, left, bottom, right} = header.getBoundingClientRect();
return {top, left, bottom, right};
}, header);
console.log(rect);
await browser.close();
})();
@emmiep
Copy link
Author

emmiep commented Feb 11, 2021

I'm afraid this script is incorrect, as .getBoundingClientRect() returns 8 elements, and the first 4 are (left, top, width, height), check here.

I don't really remember creating this gist, but I think I was only interested in those 4 values. If you want all the values you can of course return the entire thing instead.

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