Skip to content

Instantly share code, notes, and snippets.

@M4R14
Last active September 3, 2017 14:05
Show Gist options
  • Select an option

  • Save M4R14/041fd13a1440f20e8b9d84edd2bd03f7 to your computer and use it in GitHub Desktop.

Select an option

Save M4R14/041fd13a1440f20e8b9d84edd2bd03f7 to your computer and use it in GitHub Desktop.
สร้าง web Bot ด้วย Puppeteer chrome-headless ในการ Add, Del ข้อมูล
const puppeteer = require('puppeteer');
const _login = {
data:{
USERNANE: '**********',
PASSWORD: '**********'
},
elem:{
USERNANE: 'body > div.content > form > div:nth-child(4) > div > input',
PASSWORD: 'body > div.content > form > div:nth-child(5) > div > input',
SUBMIT: 'body > div.content > form > div.form-actions > button'
}
}
const login = async (page) => {
let { elem, data } = _login
await page.click(elem.USERNANE);
await page.type(data.USERNANE);
await page.click(elem.PASSWORD);
await page.type(data.PASSWORD);
await page.click(elem.SUBMIT);
await page.waitForNavigation();
}
const getTitle = async (page) => {
const title = await page.evaluate(() =>
document.getElementsByClassName('page-title')[0].firstElementChild.outerText
);
return title
}
const GOTO_Equipment_page = async (page) =>{
await page.goto('<url_Equipment_page>');
const title = await getTitle(page);
console.log('Title: ', title);
if (title == 'Equipment (เครื่องมือการรักษา)') {
return true
}
return false
}
const ADD_EQUIPMENT = async (page) => {
const BTN_ADD = 'body > div.page-container > div.page-content > div > div > div > div > div.portlet-body > div > table > thead > tr > th.text-center.col-sm-2.col-md-2 > a'
await page.click(BTN_ADD);
await page.waitForNavigation();
const title = await getTitle(page);
console.log('Title: ', title);
if (title == 'Add Equipment') {
const FORM = 'body > div.page-container > div.page-content > div > div > div > div > div > form > div.form-body'
const IS_OPEN = `${FORM} > div:nth-child(1) > div > div`;
await page.click(IS_OPEN);
const TOOL_TYPE = `${FORM} > div.form-group.form-md-line-input > div > div`;
const TOOL_TYPE_1 = `${TOOL_TYPE} > div:nth-child(1) > label`;
await page.click(TOOL_TYPE_1);
const INPUT_NMAE = `${FORM} div:nth-child(3) > div > input`
await page.click(INPUT_NMAE);
await page.type('Tool');
const INPUT_NMAE_2 = `${FORM} div:nth-child(4) > div > input`
await page.click(INPUT_NMAE_2);
await page.type('Tool_xxx');
const SELET_UNIT = `#exampleSelect1`
await page.evaluate(() => {
document.querySelector(`#exampleSelect1`).selectedIndex = 7;
});
const SUBMIT = `body > div.page-container > div.page-content > div > div > div > div > div > form > div.form-actions > div > div > button.btn.green`;
await page.click(SUBMIT);
}
await page.waitForNavigation();
}
const DEL_EQUIPMENT = async (page) => {
const BTN_DEL = `body > div.page-container > div.page-content > div > div > div > div > div.portlet-body > div > table > tbody > tr:nth-child(1) > td:nth-child(6) > form > button`;
await page.click(BTN_DEL);
const BTN_OK = `body > div.bootbox.modal.fade.bootbox-confirm.in > div > div > div.modal-footer > button.btn.btn-primary`
await page.waitForSelector(BTN_OK);
await page.click(BTN_OK);
await page.waitForNavigation();
}
const run = async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto('<url_login>');
// await page.screenshot({ path: './screenshot/first_admin.png' });
await login(page);
await GOTO_Equipment_page(page);
await page.screenshot({ path: './screenshot/EQUIPMENT.png' });
await ADD_EQUIPMENT(page);
await page.screenshot({ path: './screenshot/ADD_EQUIPMENT.png' });
await DEL_EQUIPMENT(page);
await page.screenshot({ path: './screenshot/DEL_EQUIPMENT.png' });
browser.close();
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment