简体   繁体   中英

Get the CSS attribute of an element outside the DOM world

I want to find out of the 'display' of an element outside the DOM world.

If in the DOM world, you can use getAttribute() , what would be its counter-part? I can't seem to get it using JSHandle.getProperties() or JSHandle.getProperty('display').jsonValue()

const loadingEl = await this.page.$('#app-container > main > div > div > div.infinite-loading-container > div:nth-child(1) > span')
const props = await loadingEl.getProperties()
const el = [] 
for (const property of props.values()) {
     const element = property.asElement();
           if (element) {
              el.push(element);
           }
}
console.log(el)

I'm trying to find out of the display is set to 'none'

I'd use getComputedStyle in an eval block to get the display value:

const puppeteer = require("puppeteer"); // ^19.1.0

const html = `<!DOCTYPE html><html><head>
<style>
p { display: none; }
</style>
</head>
<body>
<p>can't see me</p>
</body>
</html>`;

let browser;
(async () => {
  browser = await puppeteer.launch();
  const [page] = await browser.pages();
  await page.setContent(html);
  const display = await page.$eval(
    "p",
    el => getComputedStyle(el).display
  );
  console.log(display); // => none
})()
  .catch(err => console.error(err))
  .finally(() => browser?.close());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM