Created
February 22, 2020 14:17
-
-
Save hiiamyes/42bb6ca75f429b0d6696b38fcb617b94 to your computer and use it in GitHub Desktop.
apply-yushan-front-peak
This file contains 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 chalk = require("chalk"); | |
const readlineSync = require("readline-sync"); | |
const puppeteer = require("puppeteer"); | |
const log = console.log; | |
const itinerary = { | |
date: "109-02-28", | |
nodes: ["塔塔加登山口", "玉山前峰", "塔塔加登山口"], | |
}; | |
const applicant = { | |
name: "Yes Lee", | |
tel: "0212345678", | |
mobile: "0912345678", | |
email: "[email protected]", | |
nation: "中華民國", | |
id: "F123456789", | |
birthday: "1989-02-02", | |
contact: { | |
name: "Yes Lee 2", | |
tel: "0912345679", | |
}, | |
country: "65", | |
city: "6500007", | |
address: "約翰走路", | |
}; | |
const stay = { | |
name: "Yes Lee 2", | |
mobile: "0912223333", | |
}; | |
(async () => { | |
log(chalk.gray("Opening website...")); | |
const browser = await puppeteer.launch(); | |
const page = await browser.newPage(); | |
await page.goto( | |
"https://npm.cpami.gov.tw/apply_1_2.aspx?unit=c951cdcd-b75a-46b9-8002-8ef952ec95fd", | |
); | |
await page.click("#chk\\[\\]15"); | |
await page.click("#chk\\[\\]"); | |
await page.click("#ContentPlaceHolder1_btnagree"); | |
await page.waitFor("h2.title1"); | |
log(chalk.green("success")); | |
// itinerary | |
log(chalk.gray("Processing itinerary tab...")); | |
await page.type( | |
"#ContentPlaceHolder1_teams_name", | |
`yes${new Date().toISOString()}`, | |
); | |
await page.select("#ContentPlaceHolder1_climblinemain", "1"); | |
await page.waitFor("#ContentPlaceHolder1_climbline option[value='3']"); | |
await page.select("#ContentPlaceHolder1_climbline", "3"); | |
await page.waitFor("#ContentPlaceHolder1_sumday option[value='1']"); | |
await page.select("#ContentPlaceHolder1_sumday", "1"); | |
await page.waitFor("#ContentPlaceHolder1_applystart"); | |
await page.select("#ContentPlaceHolder1_applystart", itinerary.date); | |
await page.waitFor("#ContentPlaceHolder1_rblNode"); | |
const { nodes } = itinerary; | |
for (let i = 0; i < nodes.length; i++) { | |
const node = nodes[i]; | |
await page.$$eval( | |
"#ContentPlaceHolder1_rblNode label", | |
(labels, node) => { | |
for (let i = 0; i < labels.length; i++) { | |
const label = labels[i]; | |
if (label.textContent === node) { | |
label.click(); | |
return true; | |
} | |
} | |
}, | |
node, | |
); | |
const a = await page.waitFor( | |
i => { | |
const nodes = document.querySelectorAll( | |
"#ContentPlaceHolder1_lblSchedule a", | |
); | |
return nodes.length === i + 1; | |
}, | |
{}, | |
i, | |
); | |
} | |
await page.click("#ContentPlaceHolder1_btnover"); | |
await page.waitFor("#ContentPlaceHolder1_teams_count"); | |
await page.select("#ContentPlaceHolder1_teams_count", "1"); | |
await page.waitFor(".TABLE_g"); | |
await page.click("#ContentPlaceHolder1_snowtbChk1"); | |
await page.click("#ContentPlaceHolder1_snowtbChk2"); | |
await page.screenshot({ path: "example01.png", fullPage: true }); | |
log(chalk.green("success")); | |
// applicant | |
log(chalk.gray("Processing applicant tab...")); | |
await page.click("#menuhref1"); | |
await page.waitFor("#ContentPlaceHolder1_applycheck"); | |
await page.click("#ContentPlaceHolder1_applycheck"); | |
await page.waitFor("#ContentPlaceHolder1_apply_name"); | |
await page.type("#ContentPlaceHolder1_apply_name", applicant.name); | |
await page.type("#ContentPlaceHolder1_apply_tel", applicant.tel); | |
await page.select("#ContentPlaceHolder1_ddlapply_country", applicant.country); | |
await page.waitFor(1000); | |
await page.select("#ContentPlaceHolder1_ddlapply_city", appicant.city); | |
await page.waitFor(1000); | |
await page.type("#ContentPlaceHolder1_apply_addr", applicant.address); | |
await page.type("#ContentPlaceHolder1_apply_mobile", applicant.mobile); | |
await page.type("#ContentPlaceHolder1_apply_email", applicant.email); | |
await page.select("#ContentPlaceHolder1_apply_nation", applicant.nation); | |
await page.waitFor(1000); | |
await page.type("#ContentPlaceHolder1_apply_sid", applicant.id); | |
await page.$eval( | |
"#ContentPlaceHolder1_apply_birthday", | |
(input, applicant) => (input.value = applicant.birthday), | |
applicant, | |
); | |
await page.type( | |
"#ContentPlaceHolder1_apply_contactname", | |
applicant.contact.name, | |
); | |
await page.type( | |
"#ContentPlaceHolder1_apply_contacttel", | |
applicant.contact.tel, | |
); | |
await page.screenshot({ path: "example02.png", fullPage: true }); | |
log(chalk.green("success")); | |
// leader | |
log(chalk.gray("Processing leader tab...")); | |
await page.click("#menuhref2"); | |
await page.waitFor("#ContentPlaceHolder1_copyapply"); | |
await page.click("#ContentPlaceHolder1_copyapply"); | |
await page.waitFor( | |
applicant => | |
document.querySelector("#ContentPlaceHolder1_leader_name").value === | |
applicant.name, | |
{}, | |
applicant, | |
); | |
await page.screenshot({ path: "example03.png", fullPage: true }); | |
log(chalk.green("success")); | |
// stay | |
log(chalk.gray("Processing stay tab...")); | |
await page.click("#menuhref4"); | |
await page.waitFor("#ContentPlaceHolder1_stay_name"); | |
await page.type("#ContentPlaceHolder1_stay_name", stay.name); | |
await page.type("#ContentPlaceHolder1_stay_mobile", stay.mobile); | |
await page.screenshot({ path: "example04.png", fullPage: true }); | |
log(chalk.green("success")); | |
// Submit | |
var captchaCode = readlineSync.question("Input captchaCode..."); | |
await page.type("#ContentPlaceHolder1_vcode", captchaCode); | |
await page.screenshot({ path: "example05.png", fullPage: true }); | |
log(chalk.gray("Submitting...")); | |
await page.click("#ContentPlaceHolder1_btnsave"); | |
await page.screenshot({ path: "example06.png", fullPage: true }); | |
log(chalk.green("success")); | |
await browser.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment