node.jsheadless-browserrequestjs

How do I click a button on a website using Requests in Node JS


I want to click the test product on this page: https://biscuit-bird.myshopify.com/collections/all

I am trying to go fully request based and not use a headless browser like puppeteer or zombie as I have already done that.

Please let me know how to do this by sending a request.


Solution

  • I assume you're using this lib? https://github.com/request/request

    If so, inside a callback function that provides body of the page, you can use some lib to parse HTML content, find your link to a test product with CSS selector, and then open the URL stored in href.

    This snipped worked for me:

    const request = require('request');
    var HTMLParser = require('node-html-parser');
    
    
    request({
      url:'https://biscuit-bird.myshopify.com/collections/all',
      headers: {
        'User-Agent':'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1'
      }
    }, function (error, response, body) {
    
      // parse method returns a root of the generated DOM
      const root = HTMLParser.parse(body)
      
      // similarly to the browser's DOM, you can lookup DOM elements by their selector using method querySelectorAll  
      const links = root.querySelectorAll("a.grid-link")
    
      const href = links[0].getAttribute('href');
    
      // @TODO: send another request to the URL defined in `href`
    });