박시진

[Modifiy]Data input is possible

1 const puppeteer = require("puppeteer"); 1 const puppeteer = require("puppeteer");
2 function login(id, pw) { 2 function login(id, pw) {
3 const crawler = async() => { 3 const crawler = async() => {
4 - try{ 4 + try{
5 - const browser = await puppeteer.launch({headless: false, args:['--window-size=1920,1080']}); 5 + const browser = await puppeteer.launch({headless: false, args:['--window-size=1920,1080']});
6 - // for observing the process of login, headless -> false 6 + const page = await browser.newPage();
7 - const page = await browser.newPage(); 7 + await page.setViewport({
8 - await page.setViewport({ 8 + width:1920,
9 - width:1920, 9 + height:1080
10 - height:1080 10 + })
11 - }) 11 + await page.goto('https://e-campus.khu.ac.kr/xn-sso/login.php?auto_login=&sso_only=&cvs_lgn=&return_url=https%3A%2F%2Fe-campus.khu.ac.kr%2Fxn-sso%2Fgw-cb.php%3Ffrom%3D%26login_type%3Dstandalone%26return_url%3Dhttps%253A%252F%252Fe-campus.khu.ac.kr%252Flogin%252Fcallback');
12 - await page.goto('https://e-campus.khu.ac.kr/xn-sso/login.php?auto_login=&sso_only=&cvs_lgn=&return_url=https%3A%2F%2Fe-campus.khu.ac.kr%2Fxn-sso%2Fgw-cb.php%3Ffrom%3D%26login_type%3Dstandalone%26return_url%3Dhttps%253A%252F%252Fe-campus.khu.ac.kr%252Flogin%252Fcallback'); 12 + await page.type("#login_user_id", id);
13 - await page.evaluate(() => { 13 + await page.type("#login_user_password", pw);
14 - document.querySelector("#login_user_id").value = id 14 + await page.click('#form1 > div > div.login_btn > a > span');
15 - document.querySelector("#login_user_password").value = pw 15 + if(page.url() === 'https://e-campus.khu.ac.kr/xn-sso/gw-cb.php?from=&login_type=standalone&return_url=https%3A%2F%2Fe-campus.khu.ac.kr%2Flogin%2Fcallback'){
16 - document.querySelector('#form1 > div > div.login_btn > a > span').click(); 16 + return "Incorrect user ID or password."
17 - }) 17 + }
18 - await page.waitForTimeout(500); 18 + else {
19 - 19 + return `I got data { id:"${id}" pw:"${pw}" }`;
20 - }catch(err){ 20 + }
21 - console.log(err) 21 + }catch(err){
22 - } 22 + console.log(err)
23 - if(page.url() === 'https://e-campus.khu.ac.kr/xn-sso/login.php?auto_login=&sso_only=&cvs_lgn=&return_url=https%3A%2F%2Fe-campus.khu.ac.kr%2Fxn-sso%2Fgw-cb.php%3Ffrom%3D%26login_type%3Dstandalone%26return_url%3Dhttps%253A%252F%252Fe-campus.khu.ac.kr%252Flogin%252Fcallback'){ 23 + }
24 - return "the account id or password you entered is incorrect";}
25 - else {
26 - return `I got data { id:"${id}" pw:"${pw}" }`;
27 - }
28 } 24 }
25 +
29 crawler(); 26 crawler();
30 } 27 }
31 28
......