0

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.

reymon359
  • 1,240
  • 2
  • 11
  • 34
Salvatore Timpani
  • 407
  • 3
  • 6
  • 26

1 Answers1

0

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`
});
Alex Shchur
  • 741
  • 4
  • 13
  • this worked great! Just so I know what's happening here, can you explain what the root.querySelectorAll does, and also explain what is supposed to go inside of 'getAttribute('href');' – Salvatore Timpani Jul 13 '20 at 20:47
  • @SalvatoreTimpani, please clarify: you mean what's supposed to _after_ getAttribute('href)? Struggling to understand your request. As for root.querySelectorAll - will update my answer with some comments – Alex Shchur Jul 14 '20 at 07:58