0

I am trying to extract company name from specific website but even element exist in the html queryselector returning null I have tried using DOMContentLoaded, adding script at the bottom of the HTML and adding defer to the script but still it is not working

document.addEventListener("DOMContentLoaded", () => {
  const companyNameQuery = document.querySelector("#fixedCpName > div > div:nth-child(1) > div.col-md-5 > h1");
  const countryNameQuery = document.querySelector("#fixedCpName > div > div.row.marg-t-15 > div.col-lg-6.col-md-5 > div.pull-left > span.company-country");
  console.log(countryNameQuery);
 });
ks_
  • 11
  • 3
  • 1
    How do you know the element does exist? – Bergi Mar 02 '23 at 04:08
  • I copied the selector from the html of the page itself @Bergi – ks_ Mar 02 '23 at 04:14
  • The elements you're looking for are probably added _after_ the `DOMContentLoaded` event fires as is the case with many dynamic web pages – Phil Mar 02 '23 at 04:40
  • then what should I do solve this bug @Phil – ks_ Mar 02 '23 at 04:52
  • You have several unattractive options... add a [mutation observer](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver), set a suitably large timeout delay or poll the document for the element at regular intervals until it exists. I'd probably go for the 3rd one – Phil Mar 02 '23 at 04:56
  • @ks_ html does not contain selectors – Bergi Mar 02 '23 at 05:09

0 Answers0