how to use document.getElementById() in Nodejs

前端 未结 2 920
既然无缘
既然无缘 2020-12-20 03:27

i\'m trying to get elements by id from an html file in a js file using nodejs. I\'m getting the error \'document id not defined\' because node doesn\'t provide a document ob

相关标签:
2条回答
  • 2020-12-20 03:53

    Use JSDOM npm package:

    jsdom is a pure-JavaScript implementation of many web standards, notably the WHATWG DOM and HTML Standards, for use with Node.js. In general, the goal of the project is to emulate enough of a subset of a web browser to be useful for testing and scraping real-world web applications. https://github.com/jsdom/jsdom

    0 讨论(0)
  • 2020-12-20 04:09

    If you want to parse files within the same server you should probably use some of this options, because nodejs it's just a JavaScript implementation, There is no window or document object, see this. But to answer exactly your question:

    how can I use document.getElementById() in nodejs ?

    You could do it using Puppeteer

    Puppeteer is a Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. It can also be configured to use full (non-headless) Chrome or Chromium.

    Here a basic example:

    const puppeteer = require('puppeteer');
    
    (async () => {
        const browser = await puppeteer.launch();
    
    
            page = await browser.newPage();
            await page.goto('http://example.com/some.html', {waitUntil: 'load'});
    
    
        const newPage = await page.evaluate(() => {
    
            return  document.getElementById("idexample").innerHTML;
    
            });
    
         console.log(newPage)
    
      })();
    
    0 讨论(0)
提交回复
热议问题