title: How can I get the CSS Selectors for an element by value in NodeJS?
body: <p>I am writing a web scraper with Node and considering using a module like Cheerio or JSDom to parse the HTML into a DOM for a set of URLs. However, I have a specific functionality that is necessary.</p>
  
  <p>My objective is to build a scraper that can scrape multiple similar pages on a site for a few key pieces of information. However, I have some sample data that contains these pieces of information, and I want to use them to dynamically build a model for those pages and then scrape the rest of site using that model.</p>
  
  <p>To clarify, if there are three pages on a site, each containing a different product:</p>
  
  <p>Page 1:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  &lt;h1&gt;Product 1&lt;/h1&gt;
  &lt;p&gt;Desc&lt;/p&gt;
  &lt;small&gt;$2.05&lt;/small&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Page 2:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  &lt;h1&gt;Product 2&lt;/h1&gt;
  &lt;p&gt;Desc&lt;/p&gt;
  &lt;small&gt;$8.05&lt;/small&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Page 3:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  &lt;h1&gt;Product 3&lt;/h1&gt;
  &lt;p&gt;Desc&lt;/p&gt;
  &lt;small&gt;$5.07&lt;/small&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Say I already have the data for the first product (I know the Product Name, Desc, and Price). I want to get the selectors of each of those elements using the first page, and then use those selectors to scrape data from the other pages.</p>
  
  <p>Given the content of a tag in the DOM, how can I get the CSS Selector for that element? For example:</p>
  
  <pre><code>&lt;html&gt;
    &lt;body&gt;
      &lt;h1&gt;Hello world&lt;/h1&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>How can I provide Cheerio/JSDom with a string like "Hello world" and have it return the CSS Selector in the DOM where the element resides? </p>
  
  <p>Is there an easy way to do this (including using another framework), or is the only way just to loop through the entire DOM object and check the values of each element individually?</p>