Last active
September 20, 2019 17:10
-
-
Save peerasak-u/25d131ba7ba77c029615be8c7f3d408f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer'); | |
const track = async (number) => { | |
//launch headless browser and go to web page | |
const browser = await puppeteer.launch(); | |
const page = await browser.newPage(); | |
const url = 'http://track.thailandpost.co.th/tracking/default.aspx' | |
await page.goto(url); | |
//waiting for slider | |
await page.waitFor('.ui-draggable') | |
const slider = await page.$('.Slider'); | |
//input number | |
await page.type('input[name="TextBarcode"]', number); | |
//setting mouse cursor positions | |
const box = await slider.boundingBox(); | |
const origin = { | |
x: box.x + box.width / 2, | |
y: box.y + box.height / 2 | |
} | |
//then click and drag | |
await page.mouse.move(origin.x, origin.y); | |
await page.mouse.down(); | |
await page.mouse.move(origin.x + 140, origin.y); | |
await page.mouse.up(); | |
//waiting for results | |
await page.waitForSelector('#btBack'); | |
//parsing data | |
let status = await page.evaluate(() => { | |
let datetimeNodeList = document.querySelectorAll('td[style="width:25%;"]'); | |
let locationNodeList = document.querySelectorAll('td[style="width:23%;"]'); | |
let statusNodeList = document.querySelectorAll('td[style="width:22%;"]'); | |
let array = []; | |
for (let i = 0; i < datetimeNodeList.length; i++) { | |
array[i] = { | |
datetime: datetimeNodeList[i].innerText.replace(/\n/, ' '), | |
location: locationNodeList[i].innerText, | |
status: statusNodeList[i].innerText | |
}; | |
} | |
return array; | |
}); | |
browser.close(); | |
return status; | |
}; | |
track('EF582568151TH'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
๐ ๐