Headless chrome puppeteer

Happen to try out this “Headless Chrome Node API” from chrome today, this npm allows you to write headless chrome directly which we used to do with phantomjs or other tools, it provides useful and easy APIs,

I wrote a simple scripts allow me to check on CDC (ComfortDelGro Driving Centre) website to see if practical lessons are available now or not.

check codes below.


yarn add puppeteer
# or "npm i puppeteer"

  
  const puppeteer = require('puppeteer');

const settings = require("./settings.js");
// check every 10 second...
const timeToCheck = 10000;

(async () => {
  const {username, password} = settings;
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto("https://www.cdc.com.sg/");
  
  await page.focus("#userId");
  await page.type(username);

  await page.focus("#password");
  await page.type(password);

  await page.click("#BTNSERVICE");
  await page.waitForNavigation({waitUntil: 'load'});

  // click on Practical Lesson
  await page.click("#ctl00_Menu1_TreeView1t6");
  await page.waitForNavigation({waitUntil: 'load'});

  await page.click("#ctl00_ContentPlaceHolder1_ddlCourse");
  await page.click("#ctl00_ContentPlaceHolder1_ddlCourse option:nth-child(2)");
  await page.waitForSelector("#ctl00_ContentPlaceHolder1_updSessionAvl");
  
  const content = await page.$("#ctl00_ContentPlaceHolder1_updSessionAvl");
  try {
    const html = await page.$eval("#ctl00_ContentPlaceHolder1_gvLatestav", el => el.outerHTML);
    console.log("slots is available now!!!");
  } catch(err) {
    console.log("No slots found...");
  }
  browser.close();
})();
  

and setting.js


const settings = {
  username: "yourid",
  password: "yourpassword"
}
module.exports = settings;