Saturday 7 September 2019

Check if element is visible in DOM in Node.js

I would like to check if element is visible in DOM in Node.js. I use jsdom library for getting DOM structure. There are 2 approaches how to check element's visibility in client side javascript, but it doesn't work with jsdom in node.js.

1) offsetParent property is always null, even for visible elements

2) dom.window.getComputedStyle(el).display returns block, but element's css rule is display: none

const request = require('request');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;

request({ 'https://crooked.com/podcast-series/majority-54/', jar: true }, function (e, r, b) {
  const dom = new JSDOM(b);
  test(dom);
});

const test = (dom) => {
  const hiddenElement = dom.window.document.querySelector('.search-outer-lg');
  const visibleElement = dom.window.document.querySelector('.body-tag-inner');
  console.log(dom.window.getComputedStyle(hiddenElement).display); // block
  console.log(visibleElement.offsetParent); // null
}

Is it possible or another way how to check element's visibility in DOM in node.js?



from Check if element is visible in DOM in Node.js

No comments:

Post a Comment