如何为任何站点中的每个DOM节点创建屏幕截图?
我尝试使用无头浏览器(puppeteer),只有当我知道某些元素的XPath或Selector时它才能工作.但是如何才能为所有元素接收XPath或Selector?
async function screenshotDOMElement(opts = {}) {
const padding = 'padding' in opts ? opts.padding : 0;
const path = 'path' in opts ? opts.path : null;
const selector = opts.selector;
if (!selector)
throw Error('Please provide a selector.');
const rect = await page.evaluate(selector => {
const element =
document.evaluate(selector, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (!element)
return null;
const {x, y, width, height} = element.getBoundingClientRect();
console.log (x,y,width,height)
return {left: x, top: y, width, height, id: element.id};
}, selector);
if (!rect) …Run Code Online (Sandbox Code Playgroud)