New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: improve boundingBox method at ElementHandle #1357
Comments
Boxes can be rotated. |
@JoelEinbinder and what? Why we can't access full returned object from DevTools in puppeteer? |
@JoelEinbinder @aslushnikov what about content size? I can't get it with puppeteer. |
Right now bounding box is designed to match ( I am curious as to what your use case is for getting the content size. |
@JoelEinbinder look at this method for example: async boundingBoxNew() {
const {model} = await this._client.send('DOM.getBoxModel', {
objectId: this._remoteObject.objectId
});
if (!model)
throw new Error('Node is detached from document');
const calculatePos = function(quad) {
const x = Math.min(quad[0], quad[2], quad[4], quad[6]);
const y = Math.min(quad[1], quad[3], quad[5], quad[7]);
return {
x: x,
y: y,
width: Math.max(quad[0], quad[2], quad[4], quad[6]) - x,
height: Math.max(quad[1], quad[3], quad[5], quad[7]) - y
};
};
return {
border: calculatePos(model.border),
content: calculatePos(model.content),
margin: calculatePos(model.margin),
padding: calculatePos(model.padding),
width: model.width,
height: model.height
};
} And this sample website and script: const puppeteer = require('puppeteer');
(async() => {
const browser = await puppeteer.launch({
headless: false,
timeout: 10000
});
const page = await browser.newPage();
await page.setViewport({
width: 1280,
height: 800
});
await page.goto('https://schelkun.github.io/padding-link/index.html');
const element = await page.$('.text');
const oldBoundingBox = await element.boundingBox();
console.log(oldBoundingBox);
const newBoundingBox = await element.boundingBoxNew();
console.log(newBoundingBox);
})(); As a result I can get real content position:
Content position is very usefull for text select with mouse for example. |
This patch introduces ElementHandle.boxModel to get element's box model. Fixes #1357
This patch: - re-makes test for EH.boxModel to make it more readable - slightly changes wording in the `docs/api.md`. References puppeteer#1357.
Current version of
boundingBox
is terribly coded as I think 😄 :https://github.com/GoogleChrome/puppeteer/blob/master/lib/ElementHandle.js#L123
Look at DevTools method:
https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getBoxModel
DevTools method returns
BoxModel
:https://chromedevtools.github.io/devtools-protocol/tot/DOM/#type-BoxModel
BoxModel
contains:What If I want to get content position? Or padding position or margin position? 😢
My advice is smth like that:
The text was updated successfully, but these errors were encountered: