title: How can I check if selector exists in Puppeteer?
body: <p>In Puppeteer, how can I check if, for example, <code>#idProductType</code> exists and if not, set <code>producttype</code> to <code>&quot;&quot;</code>? I tried many many things but it doesn't work.</p>
  <pre><code>const urls = [myurls, ...]
  const productsList = [];
  for (let i = 0; i &lt; urls.length; i++) {
      const url = urls[i];
      await Promise.all([
          page.goto(url),
          page.waitForNavigation({ waitUntil: 'networkidle0' }),
      ]);
  
      let products = await page.evaluate(() =&gt; {
  
    //here i want to test if #idProductType exists do : 
          let producttype = document.querySelector('#idProductType').innerText;
    //else 
          let producttype = &quot;&quot;;
    //and same thing for other selectors
  
          let productsubtype = document.querySelector('#idProductSubType').innerText;
          let product = document.querySelector('#idProduct').innerText;
          let description = document.querySelector('td.js-orderModelVer').innerText;
          let reference = document.querySelector('td.text-nowrap').innerText;
          let prixpub = document.querySelector('td.text-nowrap.text-right.js-pricecol-msrp').innerText;
          let dispo = document.querySelector('td.text-nowrap.text-center.js-pricecol-availability').innerText;
          let retire = document.querySelector('td.js-retired-filler-cell').innerText;
  
          let results = [];
          results.push({
              producttype: producttype,
              productsubtype: productsubtype,
              product: product,
              description: description,
              reference: reference,
              prixpub: prixpub,
              dispo: dispo,
              retire: retire
          })
          return results
      })
      productsList.push(products);
  }
  </code></pre>