(async() => { // 模擬登錄 async function login(page){ console.log('正在登陸....') await page.goto('https://login.1688.com/member/signin.htm', { waitUntil: 'networkidle2', // 等待網絡空閑,在跳轉加載頁面 waitUntil: 'domcontentloaded' }) await page.waitForSelector("#loginchina > iframe") // 找到iframe const frame = (await page.frames())[1]; // 跳到iframe頁面去 await page.goto(frame.url()) // 輸入賬號密碼 await page.waitForSelector("#fm-login-id") await page.type('#fm-login-id', account, { delay: 10 }) await page.waitFor(1000) await page.waitForSelector("#fm-login-password") await page.type('#fm-login-password', pwd, { delay: 10 }) // 驗證是否有滑塊 if (await page.$('#nocaptcha-password #nc_1_n1z')) { // 獲取滑塊位置 let slidePosition = await getRect(page, "#nc_1_n1z") // 滑塊可滑動區域 let blockPosition = await getRect(page, "#nc_1__scale_text") // 鼠標初始位置 let initialX = slidePosition.x + slidePosition.width / 2 let initialY = slidePosition.y + slidePosition.height / 2 let xlength = blockPosition.width - slidePosition.width * 0.75 // 開始移動滑塊 for(let i = 0; i < 4; i++){ // await page.waitFor(1500) await move(page, initialX, initialY, xlength) await page.waitFor(1500) let errEl = await page.$("#nocaptcha-password .errloading") if(errEl){ // 出錯重置 await page.click("#nocaptcha-password .errloading a") await page.waitForSelector("#nc_1_n1z") }else{ break } } } await page.click('.fm-btn button') await page.waitForSelector(".company-name") console.log("登陸成功") } // 獲取元素位置 async function getRect(page, selector) { return await page.$eval(selector, el => { let res = el.getBoundingClientRect() return { x: res.x, y: res.y, width: res.width, height: res.height } }) } // 將鼠標移到某處 async function move(page, initialX, initialY, xlength = 0, ylength = 0) { const mouse = page.mouse await mouse.move(initialX, initialY) await mouse.down() await mouse.move(initialX + xlength, initialY + ylength, { steps: 20 }) await page.waitFor(3000) await mouse.up() } })()